Jem Jem - 2 months ago 5
Javascript Question

Javascript: Capture mouse wheel event and do not scroll the page?

I'm trying to prevent a mousewheel event captured by an element of the page to cause scrolling.

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);


Outside of this "canvas" element, the scroll needs to happen. Inside, it must only trigger the .wheel() method.
What am I doing wrong?

Thanks!
J.

Answer

You can do so by returning false at the end of your handler.

this.canvas.addEventListener('mousewheel',function(event){
    mouseController.wheel(event);
    return false; 
}, false);