简体   繁体   English

如何在 jquery 中关闭鼠标滚轮

[英]how to turn off mousewheel in jquery

I want to disable the mouse wheel in jQuery and I did it but I'm getting this error我想禁用 jQuery 中的鼠标滚轮,我做到了,但我收到了这个错误

jquery.min.js:2 [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. jquery.min.js:2 [干预] 由于目标被视为被动,因此无法阻止被动事件侦听器中的默认值。 See https://www.chromestatus.com/features/6662647093133312https://www.chromestatus.com/features/6662647093133312

This is the code in JS file:这是JS文件中的代码:

$(document).ready(() => {
  $(window).bind("mousewheel", function() {
    return false;
  });
})

The issue is because you cannot call preventDefault() on passive events, such as mousewheel is - among others.问题是因为您不能在被动事件上调用preventDefault() ,例如mousewheel是 - 等等。

To fix the issue you can set passive: false when you bind the event handler:要解决此问题,您可以在绑定事件处理程序时设置passive: false

 window.addEventListener('wheel', e => e.preventDefault(), { passive: false });
 /* just to make the scrollbar appear in this demo */ body { height: 5000px; }

However , I would strongly suggest you do not do this .但是,我强烈建议您不要这样做 Preventing users from navigating your site using the very popular mouse scroll wheel will be incredibly annoying for you users - and possibly make them believe your site is malfunctioning.阻止用户使用非常流行的鼠标滚轮浏览您的网站会让您的用户非常恼火 - 并可能让他们相信您的网站出现故障。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM