81

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
});
3
  • 3
    Chrome and IE support MouseWheelEvent, while Firefox supports WheelEvent. For listening across browser , see here. 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

4 Answers 4

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.

10
  • 2
    I'd prefer to keep the absolute value of the wheelDelta to detect speed of scroll. Is there any formula to normalize this among browsers?
    – Jan Turoň
    Commented Jul 11, 2018 at 8:08
  • Unfortunately, if you want to use the absolute value, there's no formula. You'd have to rely on some sort of table with empirically-obtained values for each browser/OS combination. For regular mice, wheelDelta is always fixed no matter how fast you scroll, so it's ok to do what I proposed above. However, if you want to capture sensitivity on a track pad, for instance (I guess that's what you're looking for), then you're own your own. Let's just hope this value gets standardized across browsers in the future. Commented Jul 11, 2018 at 13:39
  • 1
    That does throw an alert in the console: [Violation] Added non-passive event listener to a scroll-blocking 'wheel' event. Consider marking event handler as 'passive' to make the page more responsive. See chromestatus.com/feature/5745543795965952 Commented Sep 9, 2018 at 19:45
  • Interesting. I'm using Chrome 68 and am not seeing this alert. @DerkJanSpeelman Are you sure you're testing the exact same code as above? I just tried both versions (with and without delta sign extraction) but could not reproduce what you're seeing. Commented Sep 9, 2018 at 20:20
  • 1
    I'm still unable to reproduce it here. Maybe different operating systems? I'm trying it on Ubuntu 18.04. Also, are you running any extra code at the same time? Anyway, MDN mentions that passing a third parameter { passive: true } to addEventListener() solves the problem in case you don't need to event.preventDefault(). If you do, it looks like there's not much you can do (check people complaining in the comments). Commented Sep 9, 2018 at 20:55
58

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;
}
3
  • 1
    Excellent answer. Needs more attention. Many uncomplete answers on this issue,
    – Terrance00
    Commented Feb 7, 2017 at 6:16
  • This appears to be a deprecated event and has been removed from many browsers. The event to use now is a "WheelEvent" (developer.mozilla.org/en-US/docs/Web/API/WheelEvent)
    – dug
    Commented Dec 28, 2019 at 2:52
  • This is useful for older browser support, specifically IE does not support Math.sign at all, but Math.max and Math.min are supported. caniuse.com/?search=Math.sign Commented Sep 8, 2020 at 3:17
8

This will work in Firefox, Chrome and Edge too:

window.addEventListener("wheel", function(e) {
    var dir = Math.sign(e.deltaY);
    console.log(dir);
});
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.