gpt4 book ai didi

javascript - 为什么我的代码没有禁用鼠标滚轮事件?

转载 作者:行者123 更新时间:2023-11-28 14:20:06 25 4
gpt4 key购买 nike

当我在 Chrome 上滚动时,我的网站出现错误。

jquery-3.3.1.min.js:2 [干预] 由于目标被视为被动,因此无法阻止被动事件监听器内的默认行为。请参阅https://www.chromestatus.com/features/6662647093133312

为了防止这种情况发生:

 $(document).ready(function () {

$(window).on("mousewheel", function(e){
if(e.originalEvent.deltaY > 0) {
e.preventDefault();
return;
} else if (e.originalEvent.wheelDeltaY < 0) {
e.preventDefault();
return;
}

});
});

在布局/母版页中使用了它。

但它没有任何效果,错误也不会消失。

最佳答案

您应该将选项中的参数“passive”设置为“false”

MDN addEventListener

passive: A Boolean which, if true, indicates that the function specified by listener will never call preventDefault(). If a passive listener does call preventDefault(), the user agent will do nothing other than generate a console warning. See Improving scrolling performance with passive listeners to learn more.

window.addEventListener('mousewheel', function(e) {
e.preventDefault();
}, { passive: false });

关于javascript - 为什么我的代码没有禁用鼠标滚轮事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55555964/

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