国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > 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”的目標更加努力了!!!

如果有哪里寫的不對的,歡迎各位大神們指教,我會虛心學習的。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 杨浦区| 博罗县| 措美县| 梁河县| 略阳县| 抚顺市| 卢湾区| 巩义市| 黄石市| 彰化市| 太湖县| 南开区| 和顺县| 泸州市| 清涧县| 富阳市| 红河县| 盐边县| 鄯善县| 荣昌县| 绥化市| 南华县| 辽阳县| 沅陵县| 乐亭县| 宣武区| 临朐县| 彭州市| 兖州市| 高碑店市| 沂源县| 宝应县| 富蕴县| 朝阳区| 宁明县| 高淳县| 东乡县| 建宁县| 乡城县| 郸城县| 罗定市|