简体   繁体   中英

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
    }
});

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
    }
});

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
}
});

This seems to work in Safari, Chrome, and Firefox (I have not tested it in IE):

 // For Chrome window.addEventListener('mousewheel', mouseWheelEvent); // For Firefox window.addEventListener('DOMMouseScroll', mouseWheelEvent); function mouseWheelEvent(e) { var delta = e.wheelDelta ? e.wheelDelta : -e.detail; } 

From: http://www.h3xed.com/programming/javascript-mouse-scroll-wheel-events-in-firefox-and-chrome

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM