308

offsetHeightclientHeightscrollHeightまたはoffsetWidthclientWidthとの違いは何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

違いを知るには、ボックス モデルを理解する必要がありますが、基本的には次のとおりです。

クライアントの高さ:

要素の内部の高さをピクセル単位で返します。これにはパディングが含まれますが、水平スクロールバーの heightborder、またはmarginは含まれません。

オフセット高さ:

要素のborders、要素の垂直パディング、要素の水平スクロールバー(存在する場合、レンダリングされる場合)、および要素の 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>                                    

scrollHeight :ENTIRE content & padding (visible or not)
要素の高さに関係なく、すべてのコンテンツ + パディングの高さ。

clientHeight :VISIBLE content & padding
表示可能な高さのみ: 要素の明示的に定義された高さによって制限されるコンテンツ部分。

offsetHeight :VISIBLE content & padding + border + scrollbar
ドキュメント上の要素が占める高さ。

スクロールの高さ clientHeight と offsetHeight

于 2014-03-26T23:55:24.467 に答える