1.绑定点击事件
2.编写滚动函数
// 返回顶部
scollTop() {
// 设置一个定时器
const upRoll = setInterval(() => {
const top = document.documentElement.scrollTop // 每次获取页面被卷去的部分
const speed = top / 10 // 每次滚动多少 (步长值)
if (document.documentElement.scrollTop !== 0) {
document.documentElement.scrollTop -= speed // 不在顶部 每次滚动到的位置
} else {
clearInterval(upRoll) // 回到顶部清除定时器
}
}, 20)
}
- 页面调试:可以根据需要,调试定时器时间和修改步长值