I have some javascript code that fires a click event when the user scrolls up or down past a specific element (.closemenu
). I'm using this to automatically open and close a header menu when the user scrolls the page.
The problem I have is that this is firing too many times and causing lagging and glitching on scroll, I've found this post which shows the usage of throttling for scroll events but I can't get it to work with my current javascript which is:
<script>
jQuery(window).scroll(function() {
var hT = jQuery('.closemenu').offset().top,
hH = jQuery('.closemenu').outerHeight(),
wH = jQuery(window).height(),
wS = jQuery(this).scrollTop();
console.log((hT-wH) , wS);
if (wS > (hT+hH-wH) && (hT > wS) && (wS+wH > hT+hH)){
jQuery('.menu-bar').trigger('click');
}
});
</script>
I've tried a few variations but I can't work out what the issue is, does anyone know how can I throttle this event by 30ms or so?