返回顶部动画

backTop(type) {

    let timer = null;
    const { htmlSize } = this;
    console.log(htmlSize);
    cancelAnimationFrame(timer);
    timer = requestAnimationFrame(function fn() {
      const scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
      console.log(scrollTop);
      if (scrollTop > 0) {
        const gap = scrollTop - htmlSize;
        document.body.scrollTop = gap;
        document.documentElement.scrollTop = gap;
        timer = requestAnimationFrame(fn);
      } else {
        cancelAnimationFrame(timer);
      }
    });
  },

你可能感兴趣的:(返回顶部动画)