I'm using a debounce script as part of a conditional that will add some css to a side menu when a user scrolls a page beyond a certain amount of pixels and then hide the menu when they are a set amount of pixels from the bottom. The script works as intended for screen widths 992px and above (as per conditional if (debounce_bodyWidth >= 992)
) but it is still executing for dimensions below this.
Is there something about $(window).scroll
that means it is executed irrespective of conditionals?
(function ($) {
contactDebounce = function () {
var debounce_bodyWidth = $(window).width();
if (debounce_bodyWidth >= 992) {
$(window).scroll(function () {
var distanceFromBottom = $(document).height() - $(this).scrollTop();
if (distanceFromBottom <= 1200) {
$('.sticky-element').addClass('hide');
} else if ($(this).scrollTop() >= 150) {
$('.sticky-element').removeClass('hide');
$('.sticky-element').css({
'position': 'fixed',
'top': '15px'
});
} else if ($(this).scrollTop() < 150) {
$('.sticky-element').css({
'position': 'relative',
'top': '0'
});
}
});
}
}
contactDebounce();
$(window).on("debouncedresize", contactDebounce);
})(jQuery);