308

offsetHeight想解释一下, clientHeightand scrollHeightor offsetWidth, clientWidthand之间有什么区别scrollWidth

在客户端工作之前,必须知道这种差异。否则他们一半的生命将花在修复 UI 上。

Fiddle或下面的内联:

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');
}
#MainDIV {
  border: 5px solid red;
}
<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>

4

4 回答 4

690

要知道区别,您必须了解盒子模型,但基本上:

客户身高

返回元素的内部高度(以像素为单位),包括填充但包括水平滚动条高度边框边距

偏移高度

是一个测量值,包括元素边框、元素垂直填充、元素水平滚动条(如果存在,如果呈现)和元素 CSS 高度。

滚动高度

是元素内容高度的度量,包括由于溢出而在屏幕上不可见的内容


我会让它更容易:

考虑:

<element>                                     
    <!-- *content*: child nodes: -->        | content
    A child node as text node               | of
    <div id="another_child_node"></div>     | the
    ... and I am the 4th child node         | element
</element>                                    

scrollHeightENTIRE content & padding (visible or not)
所有内容的高度+填充,尽管元素的高度。

clientHeightVISIBLE content & padding
仅可见高度:内容部分受明确定义的元素高度限制。

offsetHeight :VISIBLE content & padding + border + scrollbar
文档上元素占用的高度。

滚动高度 clientHeight 和 offsetHeight

于 2014-03-26T23:55:24.467 回答
11

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

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

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

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

于 2018-08-04T18:22:19.787 回答
5

我对这三个的描述:

  • offsetHeight:元素占用了多少父级的“相对定位”空间。(即它忽略元素的position: absolute后代)
  • clientHeight:与 offset-height 相同,除了它不包括元素自己的边框、边距和其水平滚动条的高度(如果有的话)。
  • scrollHeightposition: absolute :在不滚动的情况下查看所有元素的内容/后代(包括那些)需要多少空间。

然后还有:

于 2019-10-29T20:22:19.477 回答
4

偏移量是指“某物偏离线的量或距离”。边距或边框是使 HTML 元素的实际高度或宽度“脱节”的东西。它将帮助您记住:

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

另一方面,clientHeight 与 OffsetHeight 正好相反。它不包括边框或边距。它确实包括填充,因为它位于 HTML 容器内部,因此它不能算作边距或边框等额外测量值。所以 :

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

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

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