gpt4 book ai didi

javascript - debounce 功能意味着鼠标滚轮上的 e.preventDefault 不再工作

转载 作者:行者123 更新时间:2023-11-27 22:34:38 26 4
gpt4 key购买 nike

我正在使用鼠标滚轮更改页面的背景。我只想每 1000 毫秒触发一次鼠标滚轮事件,因此我使用了防抖函数。

在我添加防抖功能并使用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);
};

最佳答案

然后像这样构建它:

$(document).ready(function() {
var changeBackground = debounce(function(e){
//code to change the background image
}, 1000)
$(document).on('mousewheel DOMMouseScroll',debounce(function(e){
e.preventDefault();
changeBackground(e);
})
});

关于javascript - debounce 功能意味着鼠标滚轮上的 e.preventDefault 不再工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39237942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com