Native js implementation returns top buffering effect

  • 2021-07-12 05:01:41
  • OfStack

Operating principle

Through the timer 30 milliseconds to perform a scroll bar rise, each rise height for the current height of 80%, thus achieving the rise buffer animation effect.

Judge that when the scroll bar height exceeds 1 screen, the button is displayed and hidden by default

Key points of knowledge


scrollTop// Get the height of the scroll bar   Write compatibility is required 
clientHeight// Visual window height   Write compatibility is required 
setInterval// Timer 
window.onscroll// Scroll trigger event 

Complete code


<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<style>
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover{text-decoration:none;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
fieldset,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
.clear{clear: both;float: none;height: 0;overflow: hidden;}
.bg{background:#9B1BC5; width: 1000px; height: 3000px; margin: 0 auto;}
#gotop{width: 50px; height: 50px; background:#5490F5; color: #fff; position: fixed; left: 50%; bottom: 30px; text-align: center; font-family: "Microsoft Yahei",tahoma,arial; font-size: 14px; cursor: pointer; margin-left: 520px; display: none;}
#gotop span{display: block;padding: 5px;}
</style>
</head>
<body>
<div class="bg"></div>
<div id="gotop"><span> Back to the top </span></div>
<script type="text/javascript">
 // Execute multiple function scenarios immediately after the page loads 
 function addloadEvent(func){
  var oldonload=window.onload;
  if(typeof window.onload !="function"){
   window.onload=func;
  }
  else{
   window.onload=function(){
    if(oldonload){
     oldonload(); 
    }
    func();
   }
  }
 }
 // Execute multiple functions immediately after the page loads. Scenario ends 
 addloadEvent(b);
 function b(){
  var gotop=document.getElementById("gotop");  
  var timer;
  var tf=true;
  // Scroll trigger 
  window.onscroll=function(){
   // Get the height of the scroll bar 
   var ostop=document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
   // Gets the height of the visible area of the window 
   //console.log(ostop)
   var ch=document.documentElement.clientHeight||document.body.clientHeight;
   // If the page exceeds 1 The screen height button is displayed, otherwise it is hidden 
   if(ostop>=ch){
    gotop.style.display="block";
   }else{
    gotop.style.display="none";
   }
   //
   if(!tf){
    clearInterval(timer);    
   }
    tf=false;
  }
  // Click trigger 
  gotop.onclick=function(){
   // Create a timer 
   timer=setInterval(function(){
    // Get the height of the scroll bar 
    var ostop=document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
    // For each rise in height 20%
    var speed=Math.ceil(ostop/5);
    // Of each rise to the current height 80%
document.documentElement.scrollTop=document.body.scrollTop=ostop-speed;
    // If the height is 0 Clear the timer 
    if(ostop==0){
     clearInterval(timer);
    } 
    tf=true;
   },30);   
  }
 }
</script>
</body>
</html>

Related articles: