SOURCE

console 命令行工具 X clear

                    
>
console
let btn = document.querySelector('.btn');
let isClick = false;
btn.addEventListener('click', function(){
  let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  console.log(scrollTop);
  let step = 50; // 歩长  一步走多远
  if(isClick){
    return;
  }
  isClick = true;
  let time = setInterval(function(){
 document.documentElement.scrollTop -= step;
 if(document.documentElement.scrollTop == 0){
      clearInterval(time);
      isClick = false;
    }
  }, 20)

  })
<div class="web">
        <div class="btn">
            点我回顶部
        </div>
    </div>
.web{
  width: 100%;
  height: 2000px;
  background-color: pink;
  position: relative;
}
.btn{
  position: fixed;
  right: 30px;
  bottom: 30px;
  background-color: skyblue;
  cursor: pointer;
}