欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

js鼠标滑轮滚动事件绑定的简单实例(兼容主流浏览器)

时间:2021-10-24 10:33:34|栏目:JavaScript代码|点击:
复制代码 代码如下:

/** Event handler for mouse wheel event.
         *鼠标滚动事件
         */ 
        var wheel = function(event) { 
            var delta = 0; 
            if (!event) /* For IE. */ 
                event = window.event; 
            if (event.wheelDelta) { /* IE/Opera. */ 
                delta = event.wheelDelta / 120; 
            } else if (event.detail) { 
                /** Mozilla case. */ 
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */ 
                delta = -event.detail / 3; 
            } 
            /** If delta is nonzero, handle it.
             * Basically, delta is now positive if wheel was scrolled up,
             * and negative, if wheel was scrolled down.
             */ 
            if (delta) 
                handle(delta); 
            /** Prevent default actions caused by mouse wheel.
             * That might be ugly, but we handle scrolls somehow
             * anyway, so don't bother here..
             */ 
            if (event.preventDefault) 
                event.preventDefault(); 
            event.returnValue = false; 
        } 

        /** Initialization code. 
         * If you use your own event management code, change it as required.
         */ 
        if (window.addEventListener) { 
            /** DOMMouseScroll is for mozilla. */ 
            window.addEventListener('DOMMouseScroll', wheel, false); 
        } 
        /** IE/Opera. */ 
        window.onmousewheel = document.onmousewheel = wheel; 

        /** This is high-level function.
         * It must react to delta being more/less than zero.
         */ 
        var handle = function(delta) { 
            var random_num = Math.floor((Math.random() * 100) + 50); 
            if (delta < 0) { 
                // alert("鼠标滑轮向下滚动:" + delta + "次!"); // 1 
                $("btn_next_pic").onclick(random_num); 
                return; 
            } else { 
                // alert("鼠标滑轮向上滚动:" + delta + "次!"); // -1 
                $("btn_last_pic").onclick(random_num); 
                return; 
            } 
        } 

上一篇:微信小程序实现日历小功能

栏    目:JavaScript代码

下一篇:js调用网络摄像头的方法

本文标题:js鼠标滑轮滚动事件绑定的简单实例(兼容主流浏览器)

本文地址:http://www.codeinn.net/misctech/181061.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有