最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mousewheel event detection not currently working in Firefox - Stack Overflow

programmeradmin1浏览0评论

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

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
    }
});
Share Improve this question asked Jun 7, 2012 at 22:45 bob_cobbbob_cobb 2,26911 gold badges52 silver badges115 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 14

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
}
});
发布评论

评论列表(0)

  1. 暂无评论