您好,欢迎访问一九零五行业门户网

JS返回顶部实例分享

本文主要为大家详细介绍了js返回顶部实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。
本文实例为大家分享了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> <p class="container"> <img src="/535e0ce800015b7511902787.jpg" alt=""> <a id="btn" href="javascript:" class="btn" title="回到顶部"></a> </p> </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); } }
相关推荐:
几种常用网页返回顶部代码
javascript简单返回顶部代码及注释说明
使用jquery实现可变透明度返回顶部效果
以上就是js返回顶部实例分享的详细内容。
其它类似信息

推荐信息