admin管理员组

文章数量:1246554

I want to capture an event when a user scrolls their scroll wheel in a situation where the page does not scroll/is not scrollable. However, the standard JS scroll event only fires when the browser actually does scroll, and will not fire for a DOM element styled to have overflow hidden.

Google maps' scroll to zoom is an example of the type of behavior I'm looking for.

Does anyone have any idea how to acplish something like this?

Thanks.

I want to capture an event when a user scrolls their scroll wheel in a situation where the page does not scroll/is not scrollable. However, the standard JS scroll event only fires when the browser actually does scroll, and will not fire for a DOM element styled to have overflow hidden.

Google maps' scroll to zoom is an example of the type of behavior I'm looking for.

Does anyone have any idea how to acplish something like this?

Thanks.

Share Improve this question asked Nov 8, 2012 at 1:43 ZevZev 1691 silver badge6 bronze badges 6
  • 1 It works in Firefox, and Chrome. Live demo: jsfiddle/chtNP/1 – Šime Vidas Commented Nov 8, 2012 at 1:47
  • This won't work too well in IE or Opera, depending on the version (see adomas/javascript-mouse-wheel) – Kelvin Commented Nov 8, 2012 at 1:49
  • @KelvinMackay I've tested in IE9, and IE8, and it works. – Šime Vidas Commented Nov 8, 2012 at 1:50
  • @ŠimeVidas everything a quick google search for "DOMMouseScroll browser support" throws up suggests otherwise... There's certainly no support for it in older versions of Firefox, Opera, IE7, etc. – Kelvin Commented Nov 8, 2012 at 1:57
  • 1 @KelvinMackay I had updated my demo to include the 'mousewheel' event. – Šime Vidas Commented Nov 8, 2012 at 2:05
 |  Show 1 more ment

3 Answers 3

Reset to default 10

You can capture the mouse-wheel event just fine:

$( '#yourElement' ).on( 'DOMMouseScroll mousewheel', function () {
    ...
});

Live demo: http://jsfiddle/chtNP/1/

(I'm using jQuery to bind the event handler, but it works regardless of which API you use, of course.)

At this time, Firefox define DOMMouseScroll and wheel events. Chrome, Opera and IE (latest, again!) define mousewheel.

This is how I did it:

if(window.onwheel !== undefined) {
    window.addEventListener('wheel', onMouseWheel)
} else if(window.onmousewheel !== undefined) {
    window.addEventListener('mousewheel', onMouseWheel)
} else {
    // unsupported browser
}

Note that addEventListener support in older IE versions needs a polyfill. Alternatively you can use the old window.mousewheel = function(){} or whatever method.

As you can see, the event listener is attached to the window object. You can't attach it to elements, in a cross-browser fashion. You can use the event object target property to see where it was triggered, and do a filter on that basis.

PS: One more cross-browser consideration: In IE, you have to use the "wheelDelta" property (and invert it's sign!) inside the event object when processing it in the callback function ("onMouseWheel"). Other browsers will populate "deltaY" (or "deltaX" for horizontal scrolling).

With jQuery, you can use this plugin, or any number of similar ones that do the same thing.

If jQuery or a similar framework isn't an option, it's possible but prepare yourself for a world of pain when you want to get it working in more than one browser...

本文标签: javascriptCapturing Scroll Wheel EventsStack Overflow