码迷,mamicode.com
首页 > 其他好文 > 详细

鼠标滚轮事件---兼容

时间:2019-01-28 20:12:11      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:javascrip   dir   body   方向   otto   ons   dev   滚轮   demo   

鼠标滚轮事件绑定及滚轮方向兼容demo

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            #test{
                width: 200px;
                height: 200px;
                background: pink;
                position: absolute;
                left: 0;
                right: 0;
                top: 0;
                bottom: 0;
                margin: auto;
            }
            
        </style>
    </head>
    <body>
        <div id="test"></div>
    </body>
    <script type="text/javascript">
        
        window.onload=function(){
            var testNode = document.querySelector("#test");

            // 事件绑定:火狐
            if(testNode.addEventListener){
                testNode.addEventListener("DOMMouseScroll",fn);
            }

            // 事件绑定:非火狐浏览器
            testNode.onmousewheel=fn;
            
            // 滚轮方向
            function fn(ev){
                ev=ev||event;
                var dir="";
                if(ev.wheelDelta){
                    dir = ev.wheelDelta>0?"up":"down";// 非火狐浏览器
                }
                if(ev.detail){
                    dir = ev.detail<0?"up":"down"; // 火狐
                }
                
                console.log(dir)
            }
            
        }
        
        
    </script>
</html>

鼠标滚轮事件---兼容

标签:javascrip   dir   body   方向   otto   ons   dev   滚轮   demo   

原文地址:https://www.cnblogs.com/zhanghua-zh/p/10330373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!