javascript 回到顶部

时间:2023-03-09 07:19:02
javascript 回到顶部
 <script type="text/javascript">
window.onload = function(){
var timer = null;
//用于判断是否是用户执行onsroll事件
var flag = true;
var oBtn = document.getElementById("btn1"); window.onscroll = function(){ //如果是用户执行onscroll事件 就清除定时器
if(!flag)
{ clearInterval(timer);
} flag = false; } oBtn.onclick = function(){ timer = setInterval(function(){
flag = true;
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
var speed = Math.floor(-scrollTop/6); document.documentElement.scrollTop= document.body.scrollTop = scrollTop + speed; if(scrollTop==0)
{
clearInterval(timer);
} },150); };
}
</script>