码迷,mamicode.com
首页 > Web开发 > 详细

原生js实现使滚动条缓慢回到顶部的效果

时间:2017-04-23 00:52:25      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:滚动   tin   time   回到顶部   document   scrolltop   speed   ott   否则   

html 部分 (因为a标签有链接功能,此时最好不要用a包裹 否则影响实现效果)

<li id="btn-top">回到顶部</li>

js部分
var top=document.getElementById("btn-top");
var bottom=document.getElementById("btn-top");
top.onclick=function(){
//alert(11);
timer=setInterval(function(){
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
var ispeed=Math.floor(-scrollTop/6);
console.log(ispeed)
if(scrollTop==0){
clearInterval(timer);
}
document.documentElement.scrollTop=document.body.scrollTop=scrollTop+ispeed;
},30)
};

原生js实现使滚动条缓慢回到顶部的效果

标签:滚动   tin   time   回到顶部   document   scrolltop   speed   ott   否则   

原文地址:http://www.cnblogs.com/yaomengli/p/6750224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!