我使用debounce script作爲條件的一部分,當用戶滾動頁面時將向側邊菜單添加一些CSS超過一定數量的像素,然後在從底部開始設置一定數量的像素時隱藏菜單。該腳本按照預期的屏幕寬度992px及以上(根據條件if (debounce_bodyWidth >= 992)
)運行,但仍在執行低於此的維度。
有沒有關於$(window).scroll
的東西,意思是它執行時不考慮條件?
(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);
你要綁定的滾動事件,每次你的窗口大於992,所以如果你超過992,那麼即使你小於992,它也會被綁定(並觸發)。你想綁定一次,然後把if條件放入事件 – Pete
如果你[調試過此代碼](http://stackoverflow.com/questions/988363/how-can-i-debug-my-java腳本代碼),你可能很容易就能自己發現它。 – Liam
乾杯皮特。現在我看到它了,我明白了。 – Luke