Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

javascript - firefox+jquery mousewheel scroll event bug

UPDATE Working fix as suggested by David (see below):

replace

    $('.scrollMe').bind("mousewheel DOMMouseScroll", ...) 

with

    $('.scrollMe').bind("mousewheel DOMMouseScroll MozMousePixelScroll", ...) 

ORIGINAL POST

Firefox 16.0.2 (latest) shows an issue when binding the "mousewheel / DOMMouseScroll" event. Scrolling with the mousewheel while the mouse pointer is on top of my targeted div causes the window to scroll as well- whilst I obviously don't want this.

I tried adding several methods to stop propagation etc. but nothing seems to work.

Javascript code:

    $(document).ready(function() {
            $('.scrollMe').bind("mousewheel DOMMouseScroll", function(e) {
                e.stopImmediatePropagation();
                e.stopPropagation();
                e.preventDefault();

                var delta = parseInt(e.originalEvent.wheelDelta || -e.originalEvent.detail);

                $(this).empty();    
                return false;
            });
    });

To see it in action, please follow the jsFiddle link below. On the example page, simply place the mouse pointer inside the div with red boxes and use your mouse's scrollwheel. Firefox will scroll the parent window the first time (unexpectedly), whilst other browsers don't.

jsFiddle Code example

Peculiar is that Firefox doesn't repeat the behaviour once you fire the event on the bound element, meaning it stops scrolling the page. However, it does repeat this behaviour after you manually scroll the page afterwards and try again.

I've also tested this in IE9 and Chrome but couldn't detect this issue in those browsers (meaning they don't scroll the window unexpectedly), so I'm guessing it's Firefox-specific (also disabled every plugin in firefox etc.)

Is this truly a bug in firefox (and if so is there a cross-browser hack that might do the trick)? Or, if you know of any other solution to achieve the same effect of catching the mousewheel event without having the page's window scroll, please feel free to answer!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can’t reproduce this bug in my FF 16.01 OSX using a touch pad (the Fiddle works fine), but I do know that there is another mozilla-specific event called MozMousePixelScroll. You might want to try to involve that as well.

There is also more information avail at MDN: https://developer.mozilla.org/en-US/docs/DOM/DOM_event_reference/DOMMouseScroll

As a sidenote, I think stopping the default action using e.preventDefault() should be enough, no need to stop the propagations as well (unless there are other IE specific bugs).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...