点击回到顶部

<script>
/*
* 固定时间回到顶部
* */
var btn = document.getElementById('btn');

btn.onclick = function () {
// clearInterval(this.timer);
if(this.running)return;
this.running = true;
var duration = 1000;
var changeL = utils.scrollT();// 当前页面卷上去的高度
var speed = changeL/duration;
this.timer = window.setInterval(() => {
var moveL = speed*30;
changeL = utils.scrollT();
if(changeL <= 0){
clearInterval(this.timer);
this.running = false;
}
document.documentElement.scrollTop -= moveL;
document.body.scrollTop -= moveL;
},30)
};
function btnDis() {
var scrT = utils.scrollT();
if(scrT > 400){
btn.style.display = 'block';
}else {
btn.style.display = 'none';
}
};
window.onscroll = function () {
btnDis();
};
window.onmousewheel = function () {
clearInterval(btn.timer);
btn.running = false;
}
</script>
原文地址:https://www.cnblogs.com/zhangyongxi/p/9613786.html