<div id="wrap" style="height: 2000px;"></div> 
<div id="top"></div>

CSS

* {
  margin: 0;
  padding: 0;
}

#top {
  width: 50px;
  height: 80px;
  background-color: skyblue;
  cursor: pointer;
  position: absolute;
  right: 10px;
  bottom: 60px;
  display: none;
}

JavaScript

window.onresize = function () {
  var cw = document.documentElement.clientWidth;
  var ch = document.documentElement.clientHeight;
  document.title = cw + '|' + ch;
}
// JS返回顶部效果
var back = document.querySelector('#top');
window.onscroll = function () {
  console.log('hello');
  // 获取页面卷上去的距离 
  var t = document.documentElement.scrollTop;
  if (t >= 600) {
    back.style.display = 'block';
  } else {
    back.style.display = 'none';
  }
  back.onclick = function () {
    document.documentElement.scrollTop = 0;
  }
}

代码演示:

202104071617779727467795