回到顶部
先在data中定义to_top=’‘
mounted() {
window.addEventListener(“scroll”, this.scrollHandle); //绑定页面滚动事件
},
//获取页面滚动高度
methods: {
scrollHandle(e) {
let top = e.srcElement.scrollingElement.scrollTop; // 获取页面滚动高度
this.to_top = top;
},
backTop(){
const timer = setInterval(() => {
const ispeed = Math.floor(-this.to_top / 5);
document.documentElement.scrollTop = document.body.scrollTop =
this.to_top + ispeed;
if (this.to_top === 0) {
clearInterval(timer);
}
}, 16);
},
},