admin管理员组文章数量:1136158
I'd like to have clean and nice JavaScript for mousewheel event, supporting only the latest version of common browsers without legacy code for obsolete versions, without any JS framework.
Mousewheel event is nicely explained here. How to simplify it for the current latest versions of the browsers?
I don't have access to all browsers to test it, so caniuse is a great help to me. Alas, mousewheel is not mentioned there.
Based on Derek's comment, I wrote this solution. Is it valid for all browsers?
someObject.addEventListener("onwheel" in document ? "wheel" : "mousewheel", function(e) {
e.wheel = e.deltaY ? -e.deltaY : e.wheelDelta/40;
// custom code
});
I'd like to have clean and nice JavaScript for mousewheel event, supporting only the latest version of common browsers without legacy code for obsolete versions, without any JS framework.
Mousewheel event is nicely explained here. How to simplify it for the current latest versions of the browsers?
I don't have access to all browsers to test it, so caniuse.com is a great help to me. Alas, mousewheel is not mentioned there.
Based on Derek's comment, I wrote this solution. Is it valid for all browsers?
someObject.addEventListener("onwheel" in document ? "wheel" : "mousewheel", function(e) {
e.wheel = e.deltaY ? -e.deltaY : e.wheelDelta/40;
// custom code
});
Share
Improve this question
edited Sep 27, 2014 at 0:54
Dan D.
74.6k15 gold badges110 silver badges127 bronze badges
asked Feb 17, 2013 at 21:37
Jan TuroňJan Turoň
32.9k23 gold badges137 silver badges177 bronze badges
3
|
4 Answers
Reset to default 109Clean and simple:
window.addEventListener("wheel", event => console.info(event.deltaY));
Browsers may return different values for the delta (for instance, Chrome returns +120
(scroll up) or -120
(scroll down). A nice trick to normalize it is to extract its sign, effectively converting it to +1
/-1
:
window.addEventListener("wheel", event => {
const delta = Math.sign(event.deltaY);
console.info(delta);
});
Reference: MDN.
Here's an article that describes this, and gives an example:
http://www.sitepoint.com/html5-javascript-mouse-wheel/
Relevant code, minus the specific example given of resizing an image:
var myitem = document.getElementById("myItem");
if (myitem.addEventListener)
{
// IE9, Chrome, Safari, Opera
myitem.addEventListener("mousewheel", MouseWheelHandler, false);
// Firefox
myitem.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
}
// IE 6/7/8
else
{
myitem.attachEvent("onmousewheel", MouseWheelHandler);
}
function MouseWheelHandler(e)
{
// cross-browser wheel delta
var e = window.event || e; // old IE support
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
return false;
}
This will work in Firefox, Chrome and Edge too:
window.addEventListener("wheel", function(e) {
var dir = Math.sign(e.deltaY);
console.log(dir);
});
This code works without an eventListener:
<body onwheel="mousewheelFunction(event)">
<script>
function mousewheelFunction(event) {
var y = event.deltaY;
let jsonObject =
{
Key: 'wheel',
Value: y
};
window.chrome.webview.postMessage(jsonObject);
}
</script>
A callback to C# WebView2 code is thrown in for anyone interested.
本文标签: javascriptMousewheel event in modern browsersStack Overflow
版权声明:本文标题:javascript - Mousewheel event in modern browsers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736959604a1957704.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
MouseWheelEvent
, while Firefox supportsWheelEvent
. For listening across browser , see here. – Derek 朕會功夫 Commented Feb 17, 2013 at 21:42