js页面回到顶部

toTop(){
  this.timer = setInterval(() => {
    let topDistance = document.documentElement.scrollTop || document.body.scrollTop;
    let moveDistance = Math.floor(-topDistance / 5);
    document.documentElement.scrollTop = document.body.scrollTop = topDistance + moveDistance;
    if (topDistance === 0) {
      clearInterval(this.timer);
    }
  }, 10);
}

你可能感兴趣的:(js小技巧,移动端)