James
James

Reputation: 31778

How can the onMouseWheel event be caught and stopped from being handled elsewhere

I would like to catch the onMouseWheel event for the whole page (for context I am doing custom scrolling) but need to stop everything else on the page handling it too. How could this be done?

Thanks in advance.

Upvotes: 1

Views: 69

Answers (2)

ATOzTOA
ATOzTOA

Reputation: 36000

You should call

event.preventDefault();

after processing the event.

Like this:

window.addEventListener("onMouseWheel", function(event) {
    // process the event for custom scrolling

    event.preventDefault();
    event.stopPropagation();
}, true);

Upvotes: 2

Licson
Licson

Reputation: 2271

You can do it in jquery like this:

$(function(){
    $('*:not(body)').on('mousewheel',function(e){
        e.preventDefault();
        e.stopPropagation();
    });
});

Alternate solution using pure javascript:

var ele = document.querySelectorAll('*:not(body)');
ele.forEach(function(i){
    ele[i].addEventListener('mousewheel',function(e){
        e.preventDefault();
        e.stopPropagation();
    },false);
});

Upvotes: 0

Related Questions