1
我正在使用鼠標滾輪瀏覽頁面的背景。我只想觸發一次1000ms的鼠標滾輪事件,因此我使用了一個去抖動功能。反跳功能意味着e.preventDefault在鼠標輪不再起作用
在添加去抖功能並使用e.preventDefault()
之前,它阻止了滾動的工作。但是,現在我添加了去抖動功能,這不再起作用,用戶可以再次滾動頁面。
請參閱下面的代碼。
$(document).ready(function() {
$(document).on('mousewheel DOMMouseScroll',debounce(function(e){
e.preventDefault();
//code to change the background image
}, 1000))
});
function debounce(fn, delay) {
var timer = null;
return function() {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function() {
fn.apply(context, args);
}, delay);
};