javascript 鼠标滚轮事件检测目前在 Firefox 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10940896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mousewheel event detection not currently working in Firefox
提问by bob_cobb
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.
出于某种原因,我在尝试识别 Firefox 中的鼠标滚轮事件时遇到了麻烦。这适用于 IE、Chrome、Safari、Opera,但不适用于 FF。我在 DOMMouseScroll 上附加了一个事件监听器,它应该在 FF 中被识别。
$(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
}
});
回答by Pointy
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:
是不正确的; 范围内没有“事件”变量。您可以直接使用“e”。jQuery 代码将确保您的处理程序获取事件参数作为参数。或者:
$(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
}
});
回答by Filipe Pinho
This code save my life.. Works in Chrome, Firefox, Edge, Internet Explorer, Opera...
这段代码救了我的命.. 适用于 Chrome、Firefox、Edge、Internet Explorer、Opera...
window.addEventListener('wheel', function(event){
if(event.deltaY < 0){
// wheeled up
}
else {
// wheeled down
}
});
回答by kdenis
This seems to work in Safari, Chrome, and Firefox (I have not tested it in IE):
这似乎适用于 Safari、Chrome 和 Firefox(我没有在 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
来自:http: //www.h3xed.com/programming/javascript-mouse-scroll-wheel-events-in-firefox-and-chrome