How do I suppress window mouse wheel scrolling…?

冷暖自知 提交于 2020-01-01 02:48:08

问题


I'm working on a canvas app embedded in a page. I have it so you can zoom into the drawing with the mousewheel but unfortunately this scrolls the page as it is part of an article.

Is it possible to prevent mousewheel scrolling on the window when I'm mousewheeling on a dom element?!


回答1:


Attach an event handler for mousewheel (Not Gecko) / DOMMouseScroll (Not IE) and prevent its default action (that is to scroll content):

if (element.addEventListener)
    element.addEventListener("DOMMouseScroll", function(event) {
        event.preventDefault();
    }, false);
else
    element.attachEvent("mousewheel", function() {
        return false;
    })

Hope this helps!




回答2:


I know this is old, but this may still be helpful to googlers.

I've written a jQuery plugin to handle this: $.disablescroll.

Not only does it handle mousewheels, but also touchmove and keypress events which would normally trigger a scroll.

// disable all scrolling:
$(window).disablescroll();

// enable scrolling again:
$(window).disablescroll("undo");


来源:https://stackoverflow.com/questions/343277/how-do-i-suppress-window-mouse-wheel-scrolling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!