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
  • 3 Chrome and IE support MouseWheelEvent, while Firefox supports WheelEvent. For listening across browser , see here. – Derek 朕會功夫 Commented Feb 17, 2013 at 21:42
  • 1 Added to Derek's point. On these cases you should really evaluate browser compatibilities. You can do that with Modernizr (modernizr.com). It will make your life a lot easier :) – user1467267 Commented Feb 17, 2013 at 22:49
  • 4 Nowdays, according to MDN the wheel event is supported in all modern desktop browsers. – gdros Commented Oct 31, 2016 at 20:59
Add a comment  | 

4 Answers 4

Reset to default 109

Clean 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