I wanted to make a smooth switching effect by rolling the mouse wheel between the first screen and the second screen. I encountered many problems. Later, with the help of kk, I finally solved the problem. I was very happy, so I recorded it. One click:
My initial code looked like this:
<!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 });
I tested this code normally under IE and Firefox, but under Google the onmousewheel event always triggers multiple times. This is an extremely annoying thing. Why does it trigger multiple times? After debugging, I found that every time we scroll the mouse, we scroll very "brutally" by a large amount at once, instead of scrolling slowly in small squares, which leads to multiple times when scrolling. Trigger the onmousewheel event and call the scrollFunc function. When the animate function in the function has not been executed, it is still called continuously. In this way, there will be a situation where the scroll bar cannot be scrolled down after scrolling multiple times and the page cannot be scrolled up. So, I changed the above js code to the following:
$(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; });
Okay, now the code can run normally, but because I am a newbie, the code is not refined enough, and kk said it again. Under his prompts, I updated the redundant code. Some modifications:
$(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; } });
I finally got the code for the introduction. I have to say that I learned a lot by solving this problem. I will work harder towards the goal of “write less, do more” in the future! ! !
If there is anything wrong with what I wrote, you are welcome to give me some advice. I will learn from it with an open mind.