vue 平滑回到顶部

methods: {
    // 回到顶部
    goTop() {
      cancelAnimationFrame(this.timer)
      const self = this
      self.timer = requestAnimationFrame(function fn () {
        const oTop = document.body.scrollTop || document.documentElement.scrollTop
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50
          self.timer = requestAnimationFrame(fn)
        } else {
          cancelAnimationFrame(self.timer)
        }
      })
    }
  }

 

posted @ 2021-12-10 16:57  卡农的忧伤ろ◆  阅读(146)  评论(0)    收藏  举报