中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

js onmousewheel事件多次觸發(fā)問題解決方法

我想做一個首屏和第二屏之間滾動鼠標滾輪就可以整平切換的效果,遇到了很多問題,后來在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(); //獲取瀏覽器窗口當前可見區(qū)域的大小    //鼠標滾動之后整屏切換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事件總是會觸發(fā)多次,這是一個極其惱人的事情,為什么會多次觸發(fā)呢?經(jīng)過調(diào)試,我發(fā)現(xiàn)是我們每次滾動鼠標時都是很“兇殘”的一下子滾動很大一個幅度,而不是一小格一小格的慢慢滾動,這就導致了滾動的時候會多次觸發(fā)onmousewheel事件,調(diào)用scrollFunc函數(shù),在函數(shù)內(nèi)的animate函數(shù)沒有執(zhí)行完的時候還是不斷的被調(diào)用,這樣就會出現(xiàn)滾動多次滾動條滾不下來頁滾不上去的情況。于是,我將上面的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;});

好了,現(xiàn)在的代碼已經(jīng)能夠正常運行了,不過由于我是一只菜鳥,代碼寫的不夠精致,又被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”的目標更加努力了!!!

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

JavaScript技術(shù)js onmousewheel事件多次觸發(fā)問題解決方法,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 亚卅毛片 | 一区二区国产精品 | 精品自拍视频在线观看 | 日本久久网 | 国产精品区一区二区三区 | 免费在线观看av网站 | 成人免费观看网站 | 日韩在线视频一区 | 午夜小影院 | 成人午夜精品一区二区三区 | 国产一区三区在线 | 欧美国产中文字幕 | 亚洲精品视频在线 | 欧美一区二区视频 | 国产成人在线视频免费观看 | 亚洲精品在线免费观看视频 | 亚洲高清久久 | 成人超碰| 亚洲国产18| 一区二区三区四区在线播放 | 国产精品海角社区在线观看 | 欧美日韩一区二区三区四区 | 中文字幕视频在线观看 | 蜜桃视频一区二区三区 | 成人av在线播放 | 欧美精品首页 | 亚洲一视频| 久久综合狠狠综合久久综合88 | 日韩精品在线播放 | 草草草草视频 | 成人精品一区二区三区中文字幕 | 久久视频精品 | 欧美在线一级 | 国产日韩一区二区三区 | 中文一区二区 | www..99re| 日日骚av | 久久久久久黄 | 久久久久国色av免费观看性色 | 日本精品裸体写真集在线观看 | 91p在线观看 |