简体   繁体   中英

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?

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);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM