更新时间:2023-10-28 21:14:16
由于函数 debounce
返回一个函数,你仍需要调用它:
Since the function debounce
returns a function, you still need to call it:
$(window).on('scroll', function (e) {
debounce(function() {
var scrollTop = $(window).scrollTop();
if (scrollTop > 50) {
$('.title').addClass('fixedPosition');
} else {
$('.title').removeClass('fixedPosition');
}
}()/*note the call here*/, 250);
});
这与将debounce逻辑包装在另一个函数中的功能不同,而函数 myLogic
将自动调用:
This is not the same as wrapping your debounce logic in another function whereas the function myLogic
will be called automatically:
function myLogic(){
var scrollTop = $(window).scrollTop();
$('.title').toggleClass('fixedPosition', scrollTop > 50);
}
$(window).on('scroll', debounce(myLogic, 250));