window.requestAnimationFrame 向右看齐 2023-01-18 04:14 183阅读 0赞 window.requestAnimationFrame() 告诉浏览器——你希望执行一个动画,并且要求浏览器在下次重绘之前调用指定的回调函数更新动画。该方法需要传入一个回调函数作为参数,该回调函数会在浏览器下一次重绘之前执行 [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) } } } [requestAnimationFrame]: https://developer.mozilla.org/zh-CN/docs/Web/API/Window/requestAnimationFrame
还没有评论,来说两句吧...