缓缓滚动到顶部

window.onload = function () {

  let btn = document.getElementById("renturn")

  var timer = null

  btn.onclick = function () {

    timer =  setInterval(function () {

       let top = document.documentElement.scrollTop || document.body.scrollTop

      var ispeed =  Math.floor(-top/8)

      console.log(top);

      if (top == 0) {

        console.log('暂停');

        clearInterval(timer)

      }

      document.documentElement.scrollTop = document.body.scrollTop = top + ispeed

    },30)
    
  }

}

你可能感兴趣的:(缓缓滚动到顶部)