【问题标题】:What is offsetHeight, clientHeight, scrollHeight?什么是 offsetHeight、clientHeight、scrollHeight?
【发布时间】:2014-05-05 16:32:30
【问题描述】:

想解释一下offsetHeightclientHeightscrollHeightoffsetWidthclientWidthscrollWidth之间的区别?

在客户端工作之前,必须知道这种差异。否则他们一半的生命将花在修复 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>

【问题讨论】:

    标签: javascript html dom offsetheight


    【解决方案1】:

    要知道区别你必须了解box model,但基本上:

    clientHeight:

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

    offsetHeight:

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

    scrollHeight:

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


    我会让事情变得更容易:

    考虑:

    <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 &amp; padding (visible or not)
    所有内容的高度 + 填充,尽管元素的高度。

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

    offsetHeightVISIBLE content &amp; padding+ border + scrollbar
    元素在文档中所占的高度。

    【讨论】:

    • 如果你只是想要可见的高度
    • clientHeight 是可见高度
    • 注意:如果元素有 position:fixed,offsetHeight 可能返回 null。 SVG offsetHeight 在 Chrome 中已被弃用。如果元素为 display:none 或具有 display:none 的祖先,则 offsetHeight 将返回 null
    • 我的scrollHeightclientHeight 都是一样的,尽管屏幕有更多的内容和一个滚动条。为什么?
    • @ZeroDarkThirty 你在 Safari 上吗?
    【解决方案2】:

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

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

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

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

    【讨论】:

      【解决方案3】:

      我对这三个的描述:

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

      那么还有:

      【讨论】:

      • 在这种情况下,关于 css 转换的注释非常重要。
      【解决方案4】:

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

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

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-05
        • 1970-01-01
        • 1970-01-01
        • 2019-03-19
        • 1970-01-01
        • 2018-05-29
        • 2011-03-23
        • 2021-03-09
        相关资源
        最近更新 更多