首页 > 语言 > JavaScript > 正文

js onmousewheel事件多次触发问题解决方法

2024-05-06 16:09:53
字体:
来源:转载
供稿:网友

做一个首屏和第二屏之间滚动鼠标滚轮就可以整平切换的效果,遇到了很多问题,下面是问题解决方法

我想做一个首屏和第二屏之间滚动鼠标滚轮就可以整平切换的效果,遇到了很多问题,后来在kk的帮助下,终于解决了这个问题,甚是欢喜,于是记录一下:

我最初的代码是这样的:

 



  1. <!DOCTYPE html> 
  2. <html lang="en"
  3. <head> 
  4. <meta charset="UTF-8" /> 
  5. <style> 
  6. div { 
  7. width: 700px; 
  8. height: 1000px; 
  9. .red { 
  10. background-color: red; 
  11. .yellow { 
  12. background-color: yellow; 
  13. </style> 
  14. </head> 
  15. <body> 
  16. <div class="red"> </div> 
  17. <div class="yellow"> </div> 
  18. <div class="red"> </div> 
  19. <div class="yellow"> </div> 
  20. <div class="red"> </div> 
  21. </body> 
  22.  
  23. <script src="../jQuery/jquery.min.js"></script> 
  24. <script src="test.js"></script> 
  25. </html> 

 

 


  1. $(document).ready(function(){ 
  2. var height = $(window).height(); //获取浏览器窗口当前可见区域的大小 
  3.     //鼠标滚动之后整屏切换 
  4. var scrollFunc = function(e){ 
  5. var scrollTop = document.body.scrollTop || document.documentElement.scrollTop; 
  6. e = e || window.event; 
  7. if((e.wheelDelta<0|| e.detail>0) && scrollTop>=0 && scrollTop<height){ //不同浏览器向下滚动  
  8. $(document.body).animate({scrollTop:height}, "fast"); 
  9. $(document.documentElement).animate({scrollTop:height}, "fast"); 
  10. }else if((e.wheelDelta>0 || e.detail<0) && scrollTop>=height && scrollTop<=height+20){ //不同浏览器向上滚动 
  11. $(document.body).animate({scrollTop:0}, "fast"); 
  12. $(document.documentElement).animate({scrollTop:0}, "fast"); 
  13. }; 
  14.     //注册事件 
  15. if(document.addEventListener){ 
  16. document.addEventListener('DOMMouseScroll',scrollFunc,false); 
  17. window.onmousewheel = document.onmousewheel = scrollFunc; //IE、chrome、safira 
  18. }); 

 

 

 

 

这样的代码我在IE和火狐下测试都是正常的,但是在谷歌下onmousewheel事件总是会触发多次,这是一个极其恼人的事情,为什么会多次触发呢?经过调试,我发现是我们每次滚动鼠标时都是很“凶残”的一下子滚动很大一个幅度,而不是一小格一小格的慢慢滚动,这就导致了滚动的时候会多次触发onmousewheel事件,调用scrollFunc函数,在函数内的animate函数没有执行完的时候还是不断的被调用,这样就会出现滚动多次滚动条滚不下来页滚不上去的情况。于是,我将上面的js代码改成了下面这样:

 


  1. $(document).ready(function(){ 
  2. var height = $(window).height(); 
  3. var scrollFunc = function(e){ 
  4. document.onmousewheel = undefined; 
  5. var scrollTop = document.body.scrollTop || document.documentElement.scrollTop; 
  6. e = e || window.event; 
  7. if((e.wheelDelta<0|| e.detail>0) && scrollTop>=0 && scrollTop<height){  
  8. $(document.body).animate({scrollTop:height}, "fast","linear",function(){ 
  9. document.onmousewheel = scrollFunc; 
  10. }); 
  11. $(document.documentElement).animate({scrollTop:height}, "fast","linear",function(){ 
  12. document.onmousewheel = scrollFunc; 
  13. }); 
  14. }else if((e.wheelDelta>0 || e.detail<0) && scrollTop>=height && scrollTop<=height+20){ 
  15. $(document.body).animate({scrollTop:0}, "fast","linear",function(){ 
  16. document.onmousewheel = scrollFunc; 
  17. }); 
  18. $(document.documentElement).animate({scrollTop:0}, "fast","linear",function(){ 
  19. document.onmousewheel = scrollFunc; 
  20. }); 
  21. }; 
  22. if(document.addEventListener){ 
  23. document.addEventListener('DOMMouseScroll',scrollFunc,false); 
  24. document.onmousewheel = scrollFunc; 
  25. }); 

 

好了,现在的代码已经能够正常运行了,不过由于我是一只菜鸟,代码写的不够精致,又被kk说了,在他的提示下,我将冗余的代码又进行了一番修改:

 


  1. $(document).ready(function(){ 
  2. var height = $(window).height(); 
  3. var width = $(window).width(); 
  4. var body; 
  5. if(navigator.userAgent.indexOf("Firefox")>0 || navigator.userAgent.indexOf("MSIE")>0){ 
  6. body = document.documentElement; 
  7. }else
  8. body = document.body; 
  9. var isFinish = true
  10. var scrollFunc = function(e){ 
  11. if(isFinish){ 
  12. var scrollTop = body.scrollTop; 
  13. e = e || window.event; 
  14. if((e.wheelDelta<0|| e.detail>0) && scrollTop>=0 && scrollTop<height-20){  
  15. scroll(height); 
  16. }else if((e.wheelDelta>0 || e.detail<0) && scrollTop>=height && scrollTop<=height+20){ 
  17. scroll(0); 
  18.  
  19. }; 
  20. var scroll = function(height){ 
  21. isFinish = false
  22. $(body).animate({scrollTop:height},"fast","linear",function(){ 
  23. isFinish = true
  24. }); 
  25. }; 
  26. if(navigator.userAgent.indexOf("Firefox")>0){ 
  27. if(document.addEventListener){ 
  28. document.addEventListener('DOMMouseScroll',scrollFunc,false); 
  29. }else
  30. document.onmousewheel = scrollFunc; 
  31. }); 

 

终于得到简介的代码了,不得不说,通过解决这个问题,还是学到很多的。以后要向着“write less, do more”的目标更加努力了!!!

如果有哪里写的不对的,欢迎各位大神们指教,我会虚心学习的。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选