阻止鼠标滚轮事件冒泡

转自点点滴滴大神;原文网址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

你可能感兴趣的:(javascript)