This article has shared the effect of javascript back to the top for your reference. The specific content is as follows
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"> <title>Back to top effect (native js)</title> <style type="text/css"> body{ margin: 0; } .bg{ width: 1022px; margin: 0 auto; } .btn{ display: none; width: 75px; height: 75px; background:url(../images/web top small icon (return to top)/return to top-085.png) no-repeat left top; position: fixed; left: 50%; margin-left: 530px; bottom: 10px; text-indent: -9999px; outline: none; } .btn:hover{ background-position: 0 -75px; } </style> <script> var timer=null; var isScroll=true; //1.2 Construct oScroll function function oScroll(){ var osTop=document.documentElement.scrollTop||document.body.scrollTop;//1.1 scroll height, compatible with var oSpeed=Math.ceil(osTop/4);//Scroll speed document.documentElement.scrollTop=document.body.scrollTop=osTop-oSpeed; if(osTop==0){//Judge the reach of the top, clean the timer clearInterval(timer); } isScroll=true; } window.onload=function() { var obtn=document.getElementById('btn');//Get the button element var clientHeight=document.documentElement.clientHeight||document.body.clientHeight; // 1. Click the return button event btn.onclick=function(){//Bind the button click event timer=setInterval(oScroll,50); } window.onscroll=function() { var osTop=document.documentElement.scrollTop||document.body.scrollTop;//1.1 scroll height, compatible if (osTop>clientHeight) { btn.style.display="block"; }else{ btn.style.display="none"; } if(!isScroll){ clearInterval(timer); } isScroll=false; } } </script></head><body> <div> <img src="../images/jz.jpg"> </div> <a href="javascript:void(0);" id="btn">Back to top</a></body></html>The above is all about this article, and I hope it will be helpful for everyone to learn JavaScript programming.