【JS基础】返回顶部效果
2017-04-17 20:46
375 查看
参考视频:http://www.imooc.com/learn/65
html/css部分
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(http://img.mukewang.com/535e0dc100010e9c00400080.jpg) no-repeat; position: fixed; left:95%; bottom: 50px; } .container a:hover{ background: url(http://img.mukewang.com/535e0dc100010e9c00400080.jpg) no-repeat; background-position: left -40px; } </style> </head> <body> <div class="container"> <img src="http://img.mukewang.com/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); } }
相关文章推荐
- js简单的点击返回顶部效果实现方法
- js实现返回顶部效果
- 用js实现简单的点击返回顶部效果
- 用js实现简单的点击返回顶部效果
- js 返回顶部效果代码
- 用js实现简单的点击返回顶部效果
- js基础练习---点击漂浮框返回浏览器顶部
- 原生js实现返回顶部缓冲效果
- JS实现效果-点击按钮返回到页面顶部
- js小效果:返回顶部
- JS做”返回顶部”按钮功能并实现滑动效果
- 分享福州网(fuzhou99.com)论坛的绝对定位浮动导航条+返回顶部 JS效果
- 返回顶部的js实现(jQuery/MooTools)
- 返回顶部效果
- ASP.NET jQuery 食谱9 (通过控件hyperlink实现返回顶部效果)
- (转)返回顶部的js实现(jQuery/MooTools)
- js 返回顶部
- js返回页面顶部
- Js返回顶部
- 常见效果实现之返回顶部(结合淡入、淡出、减速滚动)