scrollHeight,offsetHeight,clientHeight的本质区别
1).element.scrollHeight (只读)
总结
scrollHeight是指元素的滚动视图的高度(Height of the scroll view of an element,即有滚动条时从头部滚动到尾部的高度),它包括元素的内边距,但不包括外边距.
Height of the scroll view of an element; it includes the element padding but not its margin.
语法
var intElemScrollHeight = document.getElementById(id_attribute_value).scrollHeight;
描述
一个元素的scrollHeight是元素的内容(包括因为溢出而不可见的部分内容)的高度.
如果元素的内容生成垂直滚动条,则它的scrollHeight属性值等于它的clientHeight属性值.这意味着内容区需要一个滚动条或者元素含有一个overflow:visible(非滚动)的样式值.
An element's scrollHeight is a measurement of the height of an element's content including content not visible on the screen due to overflow.
If the element's content generated a vertical scrollbar, the scrollHeight value is equal to the minimum clientHeight the element would require in order to fit all the content in the viewpoint without using a vertical scrollbar. When an element's content does
not generate a vertical scrollbar, then its scrollHeight property is equal to its clientHeight property. This can mean either the content is too short to require a scrollbar or that the the element has CSS style overflow value of visible (non-scrollable).
例子
padding-top
Gentle, individualistic and very loyal, Birman cats fall between Siamese and Persian in character. If you admire cats that are non aggressive, that enjoy being with humans and tend to be on the quiet side, you may well find that Birman cats are just the felines
for you.
All Birmans have colorpointed features, dark coloration of the face,
ears, legs and tail.
Cat image and text coming fromwww.best-cat-art.com
padding-bottom
LeftTopRightBottommargin-topmargin-bottomborder-topborder-bottom
详述
scrollHeight是MSIE的DHTML模型的一部分,并不是W3C草案或技术推荐.
2).element.offsetHeight (只读)
总结
相对元素的父元素的元素高度值
Height of an element relative to the element's
offsetParent.
语法
var intElemOffsetHeight = document.getElementById(id_attribute_value).offsetHeight;
描述
元素的offsetHeightw值包括该元素的边框,元素的垂直内边距,元素的水平滚动条(若出现水平滚动条),以及元素的样式高度.
非滚动元素(没有设置overflow值,或设置为visible)将拥有相等的offsetHeight和scrollHeight值.
Typically, an element's offsetHeight 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.
Non-scrollable elements (CSS overflow not set or set to visible) will have equal offsetHeight and scrollHeight (is this right? scrollHeight doesn't include the border, while offsetHeight would include border).
For the document body object, the measurement includes total linear content height instead of the element CSS height. Floated elements extending below other linear content are ignored.
例子
padding-top
Gentle, individualistic and very loyal, Birman cats fall between Siamese and Persian in character. If you admire cats that are non aggressive, that enjoy being with humans and tend to be on the quiet side, you may well find that Birman cats are just the felines
for you.
All Birmans have colorpointed features, dark coloration of the face, ears,
legs and tail.
Cat image and text coming fromwww.best-cat-art.com
padding-bottom
LeftTopRightBottommargin-topmargin-bottomborder-topborder-bottom
详述
http://www.w3.org/TR/cssom-view/#offset-attributes
注释
offsetHeight is a property of the DHTML object model which was first introduced by MSIE. It is sometimes referred to as an element's physical/graphical dimensions, or an element's border-box height.
The example image above shows a scrollbar and an offsetHeight which fits on the window. However, non-scrollable elements may have large offsetHeight values, much larger than the visible content. These elements are typically contained within scrollable elements;
consequently these non-scrollable elements may be completely or partly invisible, depending on the scrollTop setting of the scrollable container.
3).element.clientHeight (只读)
总结
返回元素的内高度(the inner height of an element),包括内边距,但不包括水平滚动条的高度,也不包括边框及外边距.
clientHeight计算公式:
clientHeight = CSS height + CSS padding - 水平滚动条的高度(若出现水平滚动条)
Returns the inner height of an element in pixels, including padding but not the horizontal scrollbar height, border, or margin.
clientHeight can be calculated as CSS height + CSS padding - height of horizontal scrollbar (if present).
语法
var h = element.clientHeight;
例子
padding-top
Gentle, individualistic and very loyal, Birman cats fall between Siamese and Persian in character. If you admire cats that are non aggressive, that enjoy being with humans and tend to be on the quiet side, you may well find that Birman cats are just the felines
for you.
All Birmans have colorpointed features, dark coloration of the face,
ears, legs and tail.
Cat image and text coming fromhttp://www.best-cat-art.com/
padding-bottom
LeftTopRightBottommargin-topmargin-bottomborder-topborder-bottom
详述
非W3C推荐标准
注释
clientHeight is a non-standard, HTML-specific property introduced in the Internet Explorer object model.
offsetLeft returns the position the upper left edge of the element; not necessarily the 'real' left edge of the element. This is important for span elements in flowed text that wraps from one line to the next. The span may start in the middle of the page and
wrap around to the beginning of the next line. The offsetLeft will refer to the left edge of the start of the span, not the left edge of text at the start of the second line. Therefore, a box with the left, top, width and height of offsetLeft, offsetTop, offsetWidth
and offsetHeight will not be a bounding box for a span with wrapped text. (And, I can't figure out how to find the leftmost edge of such a span, sigh.)
参考及相关阅读
MSDN: scrollHeight PropertyMSDN: offsetHeight PropertyMSDN: clientHeight PropertyMSDN: Measuring Element Dimension and LocationDOM:element.scrollHeightDOM:element.offsetHeightDOM:element.clientHeight
Determining the dimensions of elements
原文:
https://developer.mozilla.org/en/DOM/element.scrollHeight
https://developer.mozilla.org/en/DOM/element.offsetHeight
https://developer.mozilla.org/en/DOM/element.clientHeight
分享到:
相关推荐
clientHeight offsetHeight scrollHeight clientWidth详解
大厂前端面试题目|# 前端基础知识 ...本章将通过多个面试题,讲解前端常考的基础知识面试题,同时复习一些重要的知识点。 ## 为何要考察 扎实的前端基础知识,是作为前端工程师的根本。基础知识能保证最基本的使用,...
在网上搜了一下,结论非常笼统,讲IE从不讲版本,因此自己做了测试并上传结论。以下结论皆是在标准模式下测试通过的,没有测试quirk模式。 clientHeight ...offsetHeight在IE6,IE7,IE8以及最新的的FF, Chr
clientHeight,offsetHeight,scrollHeight,offsetParent和parentElement,offsetLeft.
NULL 博文链接:https://love2java.iteye.com/blog/403776
四种浏览器对_clientHeight、offsetHeight、scrollHeight、clientWidth、offsetWidth_和_scrollWidth_的解
top、postop、scrolltop、offsetTop、scrollHeight、offsetHeight、clientHeight 等应用,大家可以参考下。
scrollHeight,clientHeight,offsetHeight的区别 说明: scrollHeight:DOM元素的实际内容的高度,不包border的高度,会随DOM元素中内容的增加(超过可视区后)而变大。 clientHeight:DOM元素内容可视区的高度,不包含...
clientHeight,只读 clientHeight可以用公式 CSS height + CSS padding – 水平滚动条的高度 (如果存在) 来计算。 如图,这样一个div,它的clientHeight为95,计算:50(height)+30(padding-top)+30(padding-bottom)...
尺寸相关:offsetHeight、clientHeight、scrollHeight; 偏移相关:offsetTop、clientTop、scrollTop、pageYOffset、scrollY; 获取相对视口位置:Element.getBoundingClientRect(); 获取元素的style对象:...
scrollLeft,scrollWidth,clientWidth,offsetWidth完全详解 2009年12月14日,16:58:19 | ArthurXF ...(需要提一下:CSS中的margin属性,与clientWidth、offsetWidth、clientHeight、offsetHeight均无关)
每次看到js中的 clientHeight(clientTop), offsetHeight(offsetTop),scrollHeight(scrollTop)就头大,根本分不清这几种的区别,然而碰到这些已经不 值一两次了,然后每次都要查看一下各自的区别,才能决定使用哪个...
在ie6中很多js脚本执行就会出现问题,一般是在获取clientWidth, clientHeight,offsetWidth, offsetHeight,scrollWidth, scrollHeight,scrollTop, scrollLeft会返回不正确的值。 因为在加了doctype后,在ie6中...
网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offsetWidth (包括边线和滚动条的宽); 网页可见区域高: document.body.offsetHeight (包括边线的宽); 网页正文全文宽: document...
1.四种浏览器对 document.body 的 clientHeight、offsetHeight 和 scrollHeight 的解释。这四种浏览器分别为IE(Internet Explorer)、NS(Netscape)、Opera、FF(FireFox)。clientHeight大家对 ...
getBoundingClientRect和offsetTop区别slice splice substring substr区别scrollTop、scrollHeight、offsetTop、offsetHeight、clientHeight、clientTop__proto__与prototype傻傻分不清?检测数据类型的方法原型链,...
网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offsetWidth (包括边线的宽); 网页可见区域高: document.body.offsetHeight (包括边线的宽); 网页正文全文宽: document.body....
无框架 自动调整 iframe 的大小以匹配其内容的高度并且根本没有滚动条。... iframe.contentDocument.clientHeight || 0, iframe.contentDocument.scrollHeight || 0, iframe.contentDocument.offsetHeight
document.body.clientHeight – 网页可见区域高 document.body.offsetWidth – 网页可见区域宽,包括边线和滚动条的宽 document.body.offsetHeight – 网页可见区域高,包括边线和滚动条的高[FF,chrom下是整个页面高...