我想做一個首屏和第二屏之間滾動滑鼠滾輪就可以整平切換的效果,遇到了很多問題,後來在kk的幫助下,終於解決了這個問題,甚是歡喜,於是記錄一下:
我最初的程式碼是這樣的:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> div { width: 700px; height: 1000px; } .red { background-color: red; } .yellow { background-color: yellow; } </style> </head> <body> <div class="red"> </div> <div class="yellow"> </div> <div class="red"> </div> <div class="yellow"> </div> <div class="red"> </div> </body> <script src="../jQuery/jquery.min.js"></script> <script src="test.js"></script> </html>
$(document).ready(function(){ var height = $(window).height(); //获取浏览器窗口当前可见区域的大小 //鼠标滚动之后整屏切换 var scrollFunc = function(e){ var scrollTop = document.body.scrollTop || document.documentElement.scrollTop; e = e || window.event; if((e.wheelDelta<0|| e.detail>0) && scrollTop>=0 && scrollTop<height){ //不同浏览器向下滚动 $(document.body).animate({scrollTop:height}, "fast"); $(document.documentElement).animate({scrollTop:height}, "fast"); }else if((e.wheelDelta>0 || e.detail<0) && scrollTop>=height && scrollTop<=height+20){ //不同浏览器向上滚动 $(document.body).animate({scrollTop:0}, "fast"); $(document.documentElement).animate({scrollTop:0}, "fast"); } }; //注册事件 if(document.addEventListener){ document.addEventListener('DOMMouseScroll',scrollFunc,false); } window.onmousewheel = document.onmousewheel = scrollFunc; //IE、chrome、safira });
這樣的程式碼我在IE和火狐下測試都是正常的,但是在谷歌下onmousewheel事件總是會觸發多次,這是一個極其惱人的事情,為什麼會多次觸發呢?經過調試,我發現是我們每次滾動滑鼠時都是很「兇殘」的一下子滾動很大一個幅度,而不是一小格一小格的慢慢滾動,這就導致了滾動的時候會多次觸發onmousewheel事件,調用scrollFunc函數,在函數內的animate函數沒有執行完的時候還是不斷的被調用,這樣就會出現滾動多次滾動條滾不下來頁滾不上去的情況。於是,我將上面的js程式碼改成了下面這樣:
$(document).ready(function(){ var height = $(window).height(); var scrollFunc = function(e){ document.onmousewheel = undefined; var scrollTop = document.body.scrollTop || document.documentElement.scrollTop; e = e || window.event; if((e.wheelDelta<0|| e.detail>0) && scrollTop>=0 && scrollTop<height){ $(document.body).animate({scrollTop:height}, "fast","linear",function(){ document.onmousewheel = scrollFunc; }); $(document.documentElement).animate({scrollTop:height}, "fast","linear",function(){ document.onmousewheel = scrollFunc; }); }else if((e.wheelDelta>0 || e.detail<0) && scrollTop>=height && scrollTop<=height+20){ $(document.body).animate({scrollTop:0}, "fast","linear",function(){ document.onmousewheel = scrollFunc; }); $(document.documentElement).animate({scrollTop:0}, "fast","linear",function(){ document.onmousewheel = scrollFunc; }); } }; if(document.addEventListener){ document.addEventListener('DOMMouseScroll',scrollFunc,false); } document.onmousewheel = scrollFunc; });
好了,現在的程式碼已經能夠正常運作了,不過由於我是一隻菜鳥,程式碼寫的不夠精緻,又被kk說了,在他的提示下,我將冗餘的程式碼又進行了一番修改:
$(document).ready(function(){ var height = $(window).height(); var width = $(window).width(); var body; if(navigator.userAgent.indexOf("Firefox")>0 || navigator.userAgent.indexOf("MSIE")>0){ body = document.documentElement; }else{ body = document.body; } var isFinish = true; var scrollFunc = function(e){ if(isFinish){ var scrollTop = body.scrollTop; e = e || window.event; if((e.wheelDelta<0|| e.detail>0) && scrollTop>=0 && scrollTop<height-20){ scroll(height); }else if((e.wheelDelta>0 || e.detail<0) && scrollTop>=height && scrollTop<=height+20){ scroll(0); } } }; var scroll = function(height){ isFinish = false; $(body).animate({scrollTop:height},"fast","linear",function(){ isFinish = true; }); }; if(navigator.userAgent.indexOf("Firefox")>0){ if(document.addEventListener){ document.addEventListener('DOMMouseScroll',scrollFunc,false); } }else{ document.onmousewheel = scrollFunc; } });
終於得到簡介的程式碼了,不得不說,透過解決這個問題,還是學到很多的。以後要向著「write less, do more」的目標更加努力了! ! !
如果有哪裡寫的不對的,歡迎各位大神們指教,我會虛心學習的。