Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  83] [ 5]  / answers: 1 / hits: 52192  / 12 Years ago, wed, april 25, 2012, 12:00:00

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?


More From » scroll

 Answers
21

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.


[#85991] Tuesday, April 24, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckenna

Total Points: 445
Total Questions: 109
Total Answers: 109

Location: Virgin Islands (U.S.)
Member since Sun, May 16, 2021
3 Years ago
;