阻止鼠标滚轮事件冒泡

转自点点滴滴大神;原文网址http://www.cnblogs.com/weekend001/archive/2011/05/11/2043474.html


function preventScroll(id){ var _this = document.getElementById(id); if(navigator.userAgent.indexOf("Firefox")>0){ _this.addEventListener('DOMMouseScroll',function(e){ _this.scrollTop += e.detail > 0 ? 60 : -60; e.preventDefault(); },false); }else{ _this.onmousewheel = function(e){ e = e || window.event; _this.scrollTop += e.wheelDelta > 0 ? -60 : 60; return false; }; } return this; }

参考资料:http://www.cnblogs.com/weekend001/archive/2011/05/11/2043474.html 

IE9不支持类似 e.returnValue = false  这种写法,相应的地方改成return false即可。

demo下载:http://download.csdn.net/detail/jyy_12/3688776


jquery插件:
  1. $.fn.extend({  
  2.     "preventScroll":function(){  
  3.         $(this).each(function(){  
  4.             var _this = this;  
  5.             if(navigator.userAgent.indexOf('Firefox') >= 0){   //firefox  
  6.                 _this.addEventListener('DOMMouseScroll',function(e){  
  7.                     _this.scrollTop += e.detail > 0 ? 60 : -60;     
  8.                     e.preventDefault();  
  9.                 },false);   
  10.             }else{  
  11.                 _this.onmousewheel = function(e){     
  12.                     e = e || window.event;     
  13.                     _this.scrollTop += e.wheelDelta > 0 ? -60 : 60;     
  14.                     return false;  
  15.                 };  
  16.             }  
  17.         })    
  18.     }  
  19. });  
  20. $(".box").preventScroll();  

  

转载于:https://www.cnblogs.com/little-turtle7/p/6179198.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值