-1

I need to capture mouse wheel events while the mouse is on a div or on elements within the div, and prevent the page to scroll.

There are some solutions in this forum (like this and this) but the page keeps scrolling.

The solution must be pure JavaScript (i.e. no jQuery or the like).

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
FDavidov
  • 3,505
  • 6
  • 23
  • 59

1 Answers1

3

You just need to cancel the event after capturing it.

// This disables the event for the entire document.
// Substitute a reference to the DOM element you wish to 
// deal with instead of "document" below.
document.addEventListener("mousewheel", function(evt){

  console.log("mousewheel event detected");

  evt.preventDefault(); // Cancel the event
  evt.stopPropagation() // Don't bubble

});
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71