Here's what you've learned recently to get back to top effects:
<input id= "btn1" type= "button" value= "Back to Top"/>
#btn1 {position:fixed;bottom:10px;right:10px;}
Window.onload=funcition () { var obtn=document.getelementbyid ("btn"); var timer=null; //declare a variable to see if the system or the user scrolls var Sbys=true; window.onscroll=funcition () { if (!sbys) { clearinterval (timer); } sBys=false; } obtn.onclick=funcition () { Timer = setinterval (Funcition () { //Get scrolltop var scrolltop= document.documentelement.scrolltop| | Document.body.scrolltop; //calculates the buffer speed when the button is clicked back to the top var ispeed= Math.floor (-SCROLLTOP/8); if (scrollTop= =0) { Clearinterval (timer) } sBys=true; document.documentelement.scrolltop=document.body.scrolltop=scrolltop+ ispeed; },30) }}
Knowledge Points: 1. Calculate speed (buffer) down rounding
2. Need to clear the timer when scrolltop==0
3. Need to determine whether the user or JS operation scroll bar, if the user action to clear the timer
Back to top effects