bob_cobb
bob_cobb

Reputation: 2269

Mousewheel event detection not currently working in Firefox

For some reason I'm having trouble trying to recognize the mousewheel event in Firefox. This is working in IE, Chrome, Safari, Opera but not FF. I am attaching an event listener on DOMMouseScroll, which should be recognized in FF.

Fiddle demo

$(document).unbind('mousewheel DOMMouseScroll').on('mousewheel DOMMouseScroll', function(e) {
    var evt = event || e || window.event;
    var delta = evt.detail < 0 || evt.wheelDelta > 0 ? 1 : -1;

    if (delta < 0) {
        // scroll down
    } else {
        // scroll up
    }
});

Upvotes: 3

Views: 13461

Answers (2)

Filipe Pinho
Filipe Pinho

Reputation: 31

This code save my life.. Works in Chrome, Firefox, Edge, Internet Explorer, Opera...

window.addEventListener('wheel', function(event){
if(event.deltaY < 0){
// wheeled up
}
else {
// wheeled down
}
});

Upvotes: 3

Pointy
Pointy

Reputation: 414006

Your code generates an error in the console. The line:

var evt = event || e || window.event;

is incorrect; there's no "event" variable in scope. You can just use "e" directly. The jQuery code will make sure your handler gets the event parameter as a parameter. Or:

$(document).unbind('mousewheel DOMMouseScroll').on('mousewheel DOMMouseScroll', function(evt) {
    var delta = evt.detail < 0 || evt.wheelDelta > 0 ? 1 : -1;

    if (delta < 0) {
        // scroll down
    } else {
        // scroll up
    }
});

Upvotes: 14

Related Questions