点击回到顶部

<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>
posted @ 2018-09-09 15:24  —只小白  阅读(335)  评论(0编辑  收藏  举报