天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

js onmousewheel事件多次觸發問題解決方法

我想做一個首屏和第二屏之間滾動鼠標滾輪就可以整平切換的效果,遇到了很多問題,后來在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”的目標更加努力了!!!

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

JavaScript技術js onmousewheel事件多次觸發問題解決方法,轉載需保留來源!

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

主站蜘蛛池模板: 久久视频这里只精品6国产 久久视频在线视频观品15 | se01短视频在线观看 | 白人大战34厘米黑人BD | 午夜影院一区二区三区 | 亚洲欧美国产旡码专区 | 天天躁日日躁狠狠躁中文字幕老牛 | 美国一级黄色 | 国产情侣真实露脸在线 | 成人五级毛片免费播放 | 绿巨人www| 日本黄色成年人免费观看 | 亚洲精品久久一区二区三区四区 | 凤楼app| 久久精品一区二区三区资源网 | 特黄特色大片免费播放器9 特黄特黄aaaa级毛片免费看 | 美女张开腿让男人桶爽无弹窗 | 久久夜色撩人精品国产 | 国产专区亚洲欧美另类在线 | 最近中文字幕免费高清MV视频 | 不卡一区二区高清观看视频 | 日本肉肉口番工全彩动漫 | 成年人视频在线观看免费 | 饥渴的40岁熟妇完整版在线 | 精品久久99麻豆蜜桃666 | 强姧伦久久久久久久久 | 在线A亚洲老鸭窝天堂AV高清 | 久久久久久极精品久久久 | 色宅男午夜电影网站 | 亚洲绝美精品一区二区 | 久久精品热在线观看30 | 美女内射视频WWW网站午夜 | 精品少妇爆AV无码专区 | 免费果冻传媒2021在线看 | 99人精品福利在线观看 | 亚洲伊人色综合久久天天伊人 | 无码专区久久综合久综合字幕 | 中文字幕视频免费在线观看 | 久久成人伊人欧洲精品AV | 加勒比一本之道高清视频在线观看 | 伊人亚洲综合青草青草久热 | 精品96在线观看影院 |