侧边栏壁纸
博主头像
前端学习

行动起来,活在当下

  • 累计撰写 307 篇文章
  • 累计创建 18 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

js—窗体事件

Administrator
2022-05-11 / 0 评论 / 0 点赞 / 95 阅读 / 0 字
<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

0

评论区