3

I've seen numerous examples of code that listens to mousewheel and DOMMouseEvent that assume it can only move up and down. But many mouse wheels can also move side to side. All the example code I've seen shows the same values for side to side as up and down and can't tell if it's horizontal vs vertical. Is there a way to tell?

This cannot tell horizontal vs vertical

$('#abs').bind('mousewheel DOMMouseScroll', function(e) {
    var scrollTo = 0;
    e.preventDefault();
    if (e.type == 'mousewheel') {
        scrollTo = (e.originalEvent.wheelDelta * -1);
        alert("w"+e.originalEvent.wheelDelta);
    }
    else if (e.type == 'DOMMouseScroll') {
        scrollTo = 40 * e.originalEvent.detail;
        alert("d"+e.originalEvent.detail);
    }

    //Assumes vertical
    $(this).scrollTop(scrollTo + $(this).scrollTop());

});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • Probably should be handling [Wheel Events](https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent) - just `"wheel"` and not `"mousewheel"` – Pointy Sep 14 '15 at 23:17
  • @Pointy `wheel` is only in modern browsers. Chrome didn't start supporting it until version 31. – Don Rhummy Sep 14 '15 at 23:21
  • Well as far as I know that's it. The older event types are what they are. – Pointy Sep 14 '15 at 23:26
  • http://stackoverflow.com/questions/10821985/detecting-mousewheel-on-the-x-axis-left-and-right-with-javascript – Derek Story Sep 14 '15 at 23:50
  • mouse drivers typically convert side to side to an action different than wheeling, but that replacement action can vary by platform, and none will trigger the wheel event. you can look for mousedown, scroll, and other side-effects if you need to detect such action on a given platform. – dandavis Sep 15 '15 at 00:11
  • @dandavis sorry, but you're incorrect. When I do a side mouse wheel, Firefox 40 does give me a "wheel" event. – Don Rhummy Sep 15 '15 at 00:26
  • that would be news to me but i trust you. however, that could still be the doing of the mouse driver; i had a mouse where the horizontal wheel controlled zoom, so i don't see how such an action could be standardized the same way the ubiquitous up and down wheel has been. in short, there's no simple reliable JS way of doing what you want. – dandavis Sep 15 '15 at 03:24

0 Answers0