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)
};