当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS返回顶部实例代码

JS返回顶部实例代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了js返回顶部实例代码,供大家参考,具体内容如下

html/css部分

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>document</title>
 <script src="返回顶部效果.js"></script>
<style>
 .container{
 width:1190px;
 margin: 0px auto;
 }
 .container a{
 display: none;
 width:40px;
 height:40px;
 background: url(/535e0dc100010e9c00400080.jpg) no-repeat;
 position: fixed;
 left:95%;
 bottom: 50px;
 }
 .container a:hover{
 background: url(/535e0dc100010e9c00400080.jpg) no-repeat;
 background-position: left -40px;
 }
</style> 
</head>
<body>
 <div class="container">
 <img src="/535e0ce800015b7511902787.jpg" alt="">
 <a id="btn" href="javascript:" class="btn" title="回到顶部"></a>
 </div>
</body>
</html>

js部分

window.onload=function(){
 var obtn=document.getelementbyid("btn");
 var clientheight=document.documentelement.clientheight||ocument.body.clientheight;
 var istop=true;
 var timer=null;
 window.onscroll=function(){
 var toph=document.documentelement.scrolltop||document.body.scrolltop;
 if(toph>clientheight){
  obtn.style.display="block";
 }else{
  obtn.style.display="none";
 }
 }
 obtn.onclick=function(){
 timer=setinterval(function(){
  var toph=document.documentelement.scrolltop||document.body.scrolltop;
  var steplength=math.ceil(toph/5);
  document.documentelement.scrolltop=document.body.scrolltop=toph-steplength;
  if(toph==0){
  clearinterval(timer);
  }
 },30);
 }
}

学习视频

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网