设置dom元素的scrollTop属性为0
- 直接设(可能存在闪动,看需求):
document.querySelector('.xxx').scrollTop = 0
- 动画平滑滚动:
toTop () {
let timer = null
timer = requestAnimationFrame(function fn () {
const box = document.getElementById('xxx')
let oTop = box.scrollTop
if (oTop > 0) {
box.scrollTop = oTop - 100
timer = requestAnimationFrame(fn)
} else {
cancelAnimationFrame(timer)
}
})
}
标签:box,滚动,dom,timer,JS,oTop,scrollTop
From: https://www.cnblogs.com/jia-zq/p/17185202.html