首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么是offsetHeight、clientHeight、scrollHeight?

问什么是offsetHeight、clientHeight、scrollHeight?
EN

Stack Overflow用户
提问于 2014-03-27 07:21:03
回答 3查看 173.9K关注 0票数 297

我想解释一下offsetHeight、clientHeight和scrollHeight或offsetWidth、clientWidth和scrollWidth之间的区别

在客户端工作之前,必须了解这一区别。否则,他们的一半生命将花费在修复UI上。

Fiddle,或内联如下:

​

代码语言:javascript
复制
function whatis(propType) {
  var mainDiv = document.getElementById("MainDIV");
  if (window.sampleDiv == null) {
    var div = document.createElement("div");
    window.sampleDiv = div;
  }
  div = window.sampleDiv;
  var propTypeWidth = propType.toLowerCase() + "Width";
  var propTypeHeight = propType + "Height";

  var computedStyle = window.getComputedStyle(mainDiv, null);
  var borderLeftWidth = computedStyle.getPropertyValue("border-left-width");
  var borderTopWidth = computedStyle.getPropertyValue("border-top-width");

  div.style.position = "absolute";
  div.style.left = mainDiv.offsetLeft + Math.round(parseFloat((propType == "client") ? borderLeftWidth : 0)) + "px";
  div.style.top = mainDiv.offsetTop + Math.round(parseFloat((propType == "client") ? borderTopWidth : 0)) + "px";
  div.style.height = mainDiv[propTypeHeight] + "px";
  div.style.lineHeight = mainDiv[propTypeHeight] + "px";
  div.style.width = mainDiv[propTypeWidth] + "px";
  div.style.textAlign = "center";
  div.innerHTML = propTypeWidth + " X " + propTypeHeight + "( " +
    mainDiv[propTypeWidth] + " x " + mainDiv[propTypeHeight] + " )";



  div.style.background = "rgba(0,0,255,0.5)";
  document.body.appendChild(div);

}
document.getElementById("offset").onclick = function() {
  whatis('offset');
}
document.getElementById("client").onclick = function() {
  whatis('client');
}
document.getElementById("scroll").onclick = function() {
  whatis('scroll');
}
代码语言:javascript
复制
#MainDIV {
  border: 5px solid red;
}
代码语言:javascript
复制
<button id="offset">offsetHeight & offsetWidth</button>
<button id="client">clientHeight & clientWidth</button>
<button id="scroll">scrollHeight & scrollWidth</button>

<div id="MainDIV" style="margin:auto; height:200px; width:400px; overflow:auto;">
  <div style="height:400px; width:500px; overflow:hidden;">

  </div>
</div>

​

EN

回答 3

Stack Overflow用户

发布于 2018-08-05 02:22:20

* offsetHeight是元素CSS高度的像素度量,包括边框、填充和元素的水平滚动条。

* clientHeight属性返回元素的可视高度(以像素为单位),包括填充,但不包括边框、滚动条或边距。

* scrollHeight值等于元素需要的最小高度,以便在不使用垂直滚动条的情况下适合视口中的所有内容。高度的测量方式与clientHeight相同:它包括元素的填充,但不包括元素的边框、边距或水平滚动条。

所有这些都是使用宽度而不是高度的情况。

票数 10
EN

Stack Overflow用户

发布于 2019-10-30 04:22:19

我对这三个方面的描述:

  • offsetHeight:元素占用了多少父元素的“相对定位”空间。(即它忽略与descendents)
  • clientHeight:- position: absolute相同的元素的偏移量高度,只是它排除了元素自己的边框、边距和水平滚动条的高度(如果它有one).
  • scrollHeight:,那么需要多少空间才能在不滚动的情况下查看元素的所有内容/后代(包括position: absolute内容)。

然后还有:

除了它是经过计算的after the element's css transforms are applied.之外,它与scrollHeight相同

票数 4
EN

Stack Overflow用户

发布于 2020-01-23 13:41:49

偏移量( Offset )指的是“某物偏离直线的量或距离”。边距或边框是使HTML元素的实际高度或宽度“超出行”的东西。它将帮助您记住这一点:

  • offsetHeight是元素CSS高度的像素度量,包括边框、填充和元素的水平滚动条。

另一方面,你可以说clientHeight是OffsetHeight的对立面。它不包括边框或边距。它确实包含了填充,因为它驻留在HTML容器中,所以它不会被算作额外的度量,比如边距或边框。所以:

  • clientHeight属性返回元素的可视高度(以像素为单位),包括填充,但不包括边框、滚动条或边距。

填充是所有的可滚动区域,所以你的滚动永远不会超过你的边距或边框,这就是为什么ScrollHeight不包括边距和边框,但scrollHeight有。所以:

  • scrollHeight值等于元素需要的最小高度,以便在不使用垂直滚动条的情况下适合视口中的所有内容。高度的测量方式与clientHeight相同:它包括元素的填充,但不包括其边框、边距或水平滚动条。
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22675126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档