页面按钮触发滚动置顶的功能js
//监听滚动事件
scrollHandle(e) {
// console.log(e)
this.oldScrollVal =
window.pageYOffset ||
document.documentElement.scrollTop ||
document.body.scrollTop// 兼容不同的浏览器
// console.log(this.oldScrollVal)//滚动的距离
// 存滚动的距离
this.scrollTopVal = this.oldScrollVal
},
// 触发滚动
listenerFunction(e) {
document.addEventListener('scroll', this.scrollHandle, true)
},
//点击回到页面顶部
scrollLyric() {
if (!this.title) {
const timeTop = setInterval(() => {
document.body.scrollTop = document.documentElement.scrollTop = this.scrollTopVal -= 50
if (this.scrollTopVal <= 0) {
clearInterval(timeTop)
}
}, 10)
}
}