Bruce Li
Bruce Li

Reputation: 137

Mouse wheel event difference

Please look at the following code:

<script type="text/javascript">
var scrollFunc=function(e){
    var direct=0;
    e=e || window.event;

    if(e.wheelDelta){//IE/Opera/Chrome 
        userMouse(e.wheelDelta);
    }else if(e.detail){//Firefox
        userMouse(e.wheelDelta);
    }
}

if(document.addEventListener){
    document.addEventListener('DOMMouseScroll',scrollFunc,false);
}//W3C

window.onmousewheel=document.onmousewheel=scrollFunc;//IE/Opera/Chrome/Safari

function userMouse(flage){
    if(flage == 3){
        alert("firefox UP!");
    }else if(flage == -3){
        alert("firefox DOWN!");
    }else if(flage == 120){
        alert("IE UP!");
    }else if(flage == -120){
        alert("IE DOWN!");
    }
}
</script>

The question is: if I roll the mouse wheel once,it will alert once in Firefox,but twice in IE/Chrome/Opera.I think it's kernel problem,right?How can I resolve it?

Upvotes: 1

Views: 4481

Answers (1)

gowansg
gowansg

Reputation: 795

Here is your culprit:

window.onmousewheel=document.onmousewheel=scrollFunc;

In IE, Chrome, Opera, and Safari, you're handling the onmousewheel event for both the document and the window.

You only see one alert in Firefox because, you're only handling the document's onmousewheel event here, on this line:

 if(document.addEventListener){
      document.addEventListener('DOMMouseScroll',scrollFunc,false);
 }

Upvotes: 3

Related Questions