offsetHeight, clientHeight, scrollHeight 란 무엇입니까?
차이점이 무엇인지 설명의 생각 offsetHeight
, clientHeight
그리고 scrollHeight
나 offsetWidth
, clientWidth
그리고 scrollWidth
?
클라이언트 측에서 작업하기 전에이 차이점을 알아야합니다. 그렇지 않으면 그들의 삶의 절반이 UI 수정에 소비됩니다.
바이올린 또는 인라인 아래 :
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>
차이점을 이해하려면 상자 모델 을 이해해야 하지만 기본적으로 다음과 같습니다.
패딩을 포함하지만, 화소의 요소의 내부 높이를 반환 하지 수평 스크롤바 높이 , 보더 또는 마진
is a measurement which includes the element borders, the element vertical padding, the element horizontal scrollbar (if present, if rendered) and the element CSS height.
is a measurement of the height of an element's content including content not visible on the screen due to overflow
I will make it easier:
Consider:
<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)
Height of all content + paddings, despite of height of the element.
clientHeight: VISIBLE content & padding
Only visible height: content portion limited by explicitly defined height of the element.
offsetHeight: VISIBLE content & padding
+ border + scrollbar
Height occupied by the element on document.
* offsetHeight is a measurement in pixels of the element's CSS height, including border, padding and the element's horizontal scrollbar.
* clientHeight property returns the viewable height of an element in pixels, including padding, but not the border, scrollbar or margin.
* scrollHeight value is equal to the minimum height the element would require in order to fit all the content in the viewport without using a vertical scrollbar. The height is measured in the same way as clientHeight: it includes the element's padding, but not its border, margin or horizontal scrollbar.
Same is the case for all of these with width instead of height.
참고URL : https://stackoverflow.com/questions/22675126/what-is-offsetheight-clientheight-scrollheight
'IT story' 카테고리의 다른 글
C #-키워드 사용 가상 + 재정의 vs. (0) | 2020.05.08 |
---|---|
파이썬에서 임의의 부울을 얻습니까? (0) | 2020.05.08 |
제약 조건을 일시적으로 해제 (MS SQL) (0) | 2020.05.08 |
찾고있는 파일을 제공하는 rpm 패키지를 어떻게 찾습니까? (0) | 2020.05.08 |
jQuery를 사용하여 입력 필드 유형 변경 (0) | 2020.05.08 |