问题描述
我正在尝试阻止页面元素捕获的鼠标滚轮事件导致滚动。
I'm trying to prevent a mousewheel event captured by an element of the page to cause scrolling.
我预计 false
作为获得预期结果的最后一个参数,但在此canvas元素上使用鼠标滚轮仍会导致滚动:
I expected false
as last parameter to have the expected result, but using the mouse wheel over this "canvas" element still causes scrolling:
this.canvas.addEventListener('mousewheel', function(event) {
mouseController.wheel(event);
}, false);
在这个canvas元素之外,滚动需要发生。在内部,它必须只触发 .wheel()
方法。
我做错了什么?
Outside of this "canvas" element, the scroll needs to happen. Inside, it must only trigger the .wheel()
method.What am I doing wrong?
推荐答案
你可以通过返回 false来做到这一点
在你的处理程序结束时。
You can do so by returning false
at the end of your handler.
this.canvas.addEventListener('wheel',function(event){
mouseController.wheel(event);
return false;
}, false);
更新为使用 wheel
事件为 mousewheel
对于现代浏览器已弃用,如评论中所述。
Updated to use the wheel
event as mousewheel
deprecated for modern browser as pointed out in comments.
问题是关于阻止滚动不提供正确的事件所以请检查您的浏览器支持要求,以根据需要选择合适的活动。
The question was about preventing scrolling not providing the right event so please check your browser support requirements to select the right event for your needs.
这篇关于Javascript:捕获鼠标滚轮事件,不滚动页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!