简体   繁体   中英

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.

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

This one too isn't working on firefox. I've also tried solution suggested on this webpage, but that also resulted in code only working on 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.

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

That refers to 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

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