简体   繁体   English

如何在javascript firefox中获取鼠标滚轮事件

[英]How can I get mouse wheel event in javascript firefox

I'm building an website, and I need to get mousewheel event. 我正在建立一个网站,并且需要进行鼠标滚轮事件。 In order to do that I've tried following methods: 为此,我尝试了以下方法:

//For Chrome
window.addEventListener('mousewheel', func);

// For Firefox
window.addEventListener('DOMMouseScroll', func);

This one works only on chrome. 这仅适用于chrome。

mapDiv.onmousewheel = function(e){
    func(e);
}

This one too isn't working on firefox. 这也不适用于Firefox。 I've also tried solution suggested on this webpage, but that also resulted in code only working on chrome. 我也尝试过网页上建议的解决方案,但这也导致代码仅适用于chrome。

So how to solve this issue, and also make solution compatible on as much as possible modern browsers? 那么如何解决此问题,并使解决方案在尽可能多的现代浏览器上兼容呢?

If you read the documentation, you'll see mousewheel is non-standard and suggest to use wheel event. 如果您阅读了文档,将会发现mousewheel是非标准的,并建议使用wheel事件。

See here: https://developer.mozilla.org/en-US/docs/Web/Events/mousewheel 参见此处: https : //developer.mozilla.org/en-US/docs/Web/Events/mousewheel

That refers to https://developer.mozilla.org/en-US/docs/Web/Events/wheel 指的是https://developer.mozilla.org/en-US/docs/Web/Events/wheel

Sometimes, the documentation solve the problem automagically. 有时,文档会自动解决问题。

You should use like this: 您应该这样使用:

 // standard for all browsers
 window.addEventListener('wheel', func);

And here you are an implementation example for compatibility with legacy and modern browsers: 这是与旧版和现代浏览器兼容的实现示例:

https://developer.mozilla.org/en-US/docs/Web/Events/wheel#Listening_to_this_event_across_browser https://developer.mozilla.org/zh-CN/docs/Web/Events/wheel#Listening_to_this_event_across_browser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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