Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 720
Jem Avatar asked Apr 25 '12 09:04

Jem


People also ask

How do I stop my mouse wheel from scrolling in jquery?

Try using . unmousewheel() , it should also work.

Does scrollIntoView trigger scroll event?

scrollIntoView does not trigger mousewheel nor scroll event in Angular. Bookmark this question.

How do I get the mouse wheel event?

The onwheel event occurs when the mouse wheel is rolled up or down over an element. The onwheel event also occurs when the user scrolls or zooms in or out of an element by using a touchpad (like the "mouse" of a laptop).

How do I assign a click to scroll wheel?

Go to the normal mouse tab, add a new button, go to the "click here to select mouse button" area and scroll the wheel. It will capture that action and you may assign it to what you want.


1 Answers

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

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

Or using event.preventDefault()

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

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.

Updated a second time with a more modern approach option.

like image 128
GillesC Avatar answered Sep 18 '22 18:09

GillesC