window.requestAnimationFrame() 告诉浏览器——你希望执行一个动画,并且要求浏览器在下次重绘之前调用指定的回调函数更新动画。该方法需要传入一个回调函数作为参数,该回调函数会在浏览器下一次重绘之前执行
// 返回顶部
backToTop() {
const smoothscroll = () => {
const body = this.$refs.body
const currentScroll = body.scrollTop
if (currentScroll > 0) {
window.requestAnimationFrame(smoothscroll)
if (typeof body.scrollTo === 'function') {
body.scrollTo(0, currentScroll - (currentScroll / 5))
} else {
body.scrollTop = currentScroll - (currentScroll / 5)
}
}
}