一张图带你了解 JavaScript DOM、事件的尺寸和位置

2020-02-02  本文已影响0人  wdapp

本文主要总结整理了事件参数、Window、Element中的位置和尺寸属性

一张图带你了解 JavaScript DOM、事件的尺寸和位置

DOM尺寸&位置.png

event

window

box

示例完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>一张图带你了解 JavaScript DOM、事件的尺寸和位置</title>
  <style>
  * {
    margin: 0;
    padding: 0;
  }

  .container {
    border: 1px red solid;
    position: absolute;
    width: 100%;
    height: 1500px;
    box-sizing: border-box;
  }

  .container .info {
    position: fixed;
    z-index: 1;
  }

  .container .box {
    width: 200px;
    height: 200px;
    border: 30px green solid;
    padding: 20px;
    margin: 20px;
    position: absolute;
    left: 100px;
    top: 200px;
  }

  .container .box::after {
    content: "";
    display: inline-block;
    position: absolute;
    top: 150px;
    left: 150px;
    border: 5px red solid;
    border-radius: 50%;
  }

  .container .box .pre {
    height: 100%;
    border: 1px pink solid;
    box-sizing: border-box;
  }

  .container .wrap {
    width: 200px;
    height: 150px;
    border: 20px blue solid;
    padding: 10px;
    margin: 20px;
    position: absolute;
    left: 100px;
    top: 600px;
    overflow: auto;
  }

  .container .wrap .pre {
    height: 350px;
    border: 1px pink solid;
    box-sizing: border-box;
  }

  .group {
    position: absolute;
    top: 610px;
    right: 100px;
  }
  </style>
</head>
<body>

<div class="container" id="container">
  <pre class="info">
.container {
  border: 1px red solid;
  position: absolute;
  width: 100%;
  height: 1500px;
  box-sizing: border-box;
}
  </pre>
  <div class="box" id="box">
    <pre class="pre">
.container .box {
  width: 200px;
  height: 200px;
  border: 30px green solid;
  padding: 20px;
  margin: 20px;
  position: absolute;
  left: 100px;
  top: 200px;
}
    </pre>
  </div>
  <div class="wrap" id="wrap">
    <pre class="pre">
.container .wrap {
    width: 200px;
    height: 150px;
    border: 20px blue solid;
    padding: 10px;
    margin: 20px;
    position: absolute;
    left: 100px;
    top: 600px;
  }
    </pre>
  </div>
</div>

<div class="group">
<pre class="pre">
.container .wrap {
  width: 200px;
  height: 150px;
  border: 20px blue solid;
  padding: 10px;
  margin: 20px;
  position: absolute;
  left: 100px;
  top: 600px;
}
</pre>
  <pre class="pre">
.container .wrap .pre {
  height: 350px;
  border: 1px pink solid;
  box-sizing: border-box;
}
</pre>
</div>


<script>
var container = document.getElementById("container");
var box = document.getElementById("box");
var wrap = document.getElementById("wrap");
box.addEventListener("click", function(e) {
  console.log("box click event", e);
  console.log("box click e.target", e.target);
  console.log("box click e.screenY", e.screenY);
  console.log("box click e.clientY", e.clientY);
  console.log("box click e.pageY", e.pageY);
  console.log("box click e.offsetY", e.offsetY);
  console.log("box click e.layerY", e.layerY);

  console.log("window.scrollY", window.scrollY);
  console.log("window.screenY", window.screenY);
  console.log("window.outerHeight", window.outerHeight);
  console.log("window.innerHeight", window.innerHeight);
  console.log("window.screen.availHeight", window.screen.availHeight);
  console.log("window.screen.height", window.screen.height);
  console.log("window.screen.availTop", window.screen.availTop);

  console.log("box.clientHeight", box.clientHeight);
  console.log("box.clientTop", box.clientTop);
  console.log("box.scrollHeight", box.scrollHeight);
  console.log("box.scrollTop", box.scrollTop);
  console.log("box.offsetTop", box.offsetTop);
  console.log("box.offsetHeight", box.offsetHeight);
});

window.addEventListener("scroll", function() {
  //声明 <!DOCTYPE html>时使用
  console.log("document.documentElement.scrollTop", document.documentElement.scrollHeight);
  console.log("document.documentElement.scrollTop", document.documentElement.scrollTop);
  //未声明 <!DOCTYPE html> 时使用
  // console.log("document.body.scrollTop", document.body.scrollTop);
  // console.log("document.body.scrollHeight", document.body.scrollHeight);
});

wrap.addEventListener("scroll", function() {
  console.log("wrap.scrollHeight", wrap.scrollHeight);
  console.log("wrap.scrollTop", wrap.scrollTop);
});

setTimeout(function(){
  window.scrollTo(0, 119);
  wrap.scrollTop = 100;
},1000)

</script>
</body>
</html>

上一篇 下一篇

猜你喜欢

热点阅读