在网上搜了一下,结论非常笼统,讲IE从不讲版本,因此自己做了测试并上传结论。以下结论皆是在标准模式下测试通过的,没有测试quirk模式。

clientHeight

大部分浏览器对 clientHeight 都没有什么异议,认为是元素可视区域的高度,也就是说元素或窗口中可以看到内容的这个区域的高度,即然是指可看到内容的区域,滚动条不算在内。但要注意padding是算在内。其计算方式为clientHeight = topPadding + bottomPadding+ height - 水平滚动条高度。

offsetHeight

在IE6,IE7,IE8, IE9以及最新的的FF, Chrome中,对于一般元素,都是offsetHeight = padding + height + border = clientHeight + 滚动条 + 边框。

scrollHeight

scrollHeight的争议比较大,有些浏览器认为scrollHeight可以小于clientHeight,有些认为scrollHeight至少应该等于clientHeight。但有一点是一样的,就是scrollHeight >= topPadding + bottomPadding + 内容margin box的高度。

在浏览器中的区别在于:

IE6、IE7 认为scrollHeight 是内容高度,可以小于clientHeight。

FF 认为scrollHeight 是内容高度,不过最小值是clientHeight。

注: 以上都是对于一般元素而方言的,body和documentElement的clientHeight, offsetHeight和scrollHeight在各个浏览器中的计算方式又不同。

在所有的浏览器中,如果你想获取视窗可见部分的高度,应该使用documentElement.clientHeight,因为body.clientHeight是由它的内容决定的。

FF30

注意:Firefox30中,水平滚动条的宽度是17个像素。

body

offsetHeight = body.padding + body.border + body.height(CSS设置或内容撑的);

clientHeight = body.padding + body.height(CSS设置或内容撑的);

scrollHeight >= clientHeight;

documentElement

offsetHeight = body.offsetHeight + body.margin;

clientHeight = window窗口可见高度;

scrollHeight >= clientHeight

因此,只是获取窗口可见高度,在FF中得用documentElement.clientHeight,获取整个页面的高度,则应该用documentElement.scrollHeight。

元素

offsetHeight = padding +border + height;

clientHeight = padding +height -水平滚动条的高度。

scrollHeight >=clientHeight

总结:从body, documentElement, 元素的结果分析,FireFox认为scrollHeight的最小高度是clientHeight。

offsetLeft = 元素border左上角到window视窗原点的距离 或 到offsetParent的border box顶部的距离。

Chrome 39

注意:Chrome39中,水平滚动条的宽度是17个像素。

body

offsetHeight = body.padding+ body.border + body.height(CSS设置或内容撑大);

clientHeight= body.pdding + body.height(CSS设置或内容撑大);

scrollHeight >= offsetHeight; 并且scrollHeight >= window窗口可见高度;

如果body没有内容(空的):

body.offsetHeight == documentElement.offsetHeight;

body.clientHeight ==documentElement.clientHeight;

body.scrollHeight ==documentElement.scrollHeight;

而且以上属性的值都是浏览器的视窗高度。

documentElement

offsetHeight = scrollHeight = body.offsetHeight+ body.margin;

clientHeight = window视窗可见高度;

如果body内容过短,则documentElement的offsetHeight和scrollHeight将比clientHeight小。

因此,只是获取页面窗口可视部分高度,在Chrome中用documentElement.clientHeight;获取整个页面内容最大高度(如果比窗口小,取窗口的高度),则应该用body.scrollHeight;获取页面内容的实际高度,应该使用body.offsetHeight()。

元素

offsetHeight = padding + border + height;

clientHeight = padding + height -水平滚动条的高度;

scrollHeight >= clientHeight;

offsetLeft = 元素border左上角到画布原点的距离 或 到offsetParent的border box顶部的距离。

IE9

注意:IE9中,滚动条的宽度是17个像素。

body

offsetHeight = body.padding +body.border + body.height(CSS设置或内容撑大);

clientHeight =  body.padding + body.height(CSS设置或内容撑大);

scrollHeight >= clientHeight;

documentElement

offsetHeight = clientHeight + 水平滚动条的高度;

clientHeight = window窗口可见高度

scrollHeight >= clientHeight并且scrollHeight >= body.offsetHeight

因此,只是获取window窗口可见高度,在IE9中得用documentElement.clientHeight,获取整个页面内容的高度,则应该用documentElement.scrollHeight。

元素

offsetHeight = padding +border + height。

clientHeight = padding +height - 滚动条的宽度。

scrollHeight >=clientHeight;

总结:从body, documentElement, 元素的结果分析,IE9认为scrollHeight的最小高度是clientHeight。

从结果分析,IE9认为scrollHeight的最小高度是clientHeight。

IE8

注意:IE8中,滚动条的宽度是17个像素。

body

offsetHeight = body.padding +body.border + body.height(CSS设置或内容撑大);

clientHeight =  body.padding + body.height(CSS设置或内容撑大);

scrollHeight >= clientHeight;

documentElement

offsetHeight = clientHeight + 水平滚动条的高度 + body.border

clientHeight = window窗口可见高度

scrollHeight >= clientHeight并且scrollHeight >= body.offsetHeight

因此,只是获取窗口可见高度,在IE8中得用documentElement.clientHeight,获取整个页面内容高度,则应该用documentElement.scrollHeight。

元素上

offsetHeight = padding +border + height。

clientHeight = padding +height – 水平滚动条高度。

scrollHeight >=clientHeight

从结果分析,IE8认为scrollHeight的最小高度是clientHeight。

offsetLeft = 元素border左上角到画布原点的距离 或 到offsetParent的border box顶部的距离。

IE7

注意:IE7中,滚动条的宽度是17个像素。

body

offsetHeight = body.padding + body.border+ body.height(CSS设置或内容撑大);

clientHeight = body.height +body.padding – 水平滚动条高度;

scrollHeight =  内容margin box的高度;

documentElement

offsetHeight = clientHeight =window视窗可见高度;

scrollHeight = body.offsetHeight+ body.margin;

因此,只是获取窗口可见部分高度,在IE7中得用documentElement.clientHeight,获取整个页面内容的大小,则用documentElement.scrollHeight。

元素

offsetHeight = padding +border + height。

clientHeight = padding +height - scrollbar.width。

scrollHeight = padding + 内容margin box的高度

从结果分析,IE7认为scrollHeight 可以小于clientHeight。

offsetLeft = 元素border box左上角到父容器(不是offsetParent)的border box左上角的距离。

IE6

body

offsetHeight = body.padding +内容margin box的高度。

clientHeight = scrollHeight

documentElement

offsetHeight=画布高度,但offsetHeight>= clientHeight

clientHeight = window窗口可见高度。

scrollHeight = 内容的高度

因此,只是获取页面窗口的大小,在IE6中得用documentElement.clientHeight,获取整个页面内容的大小,则应该用documentElement.offsetHeight。

元素

offsetHeight = padding +border + height。

clientHeight = padding +height - scrollbar.width。

scrollHeight = padding + 内容margin box的高度

从结果分析,IE6认为scrollHeight 可以小于clientHeight。

offsetLeft = 元素border box左上角到父容器(不是offsetParent)的border box左上角的距离。

结论

l  IE6、IE7认为scrollHeight可以小于clientHeight;

l  IE8、IE9和Firefox认为scrollHeight>=clientHeight;

l  取窗口可见部分高度,统一用documentElement.clientHeight即可;

l 取页面内容的高度(如果内容高度比窗口高度小,取窗口高度),则用documentElement.scrollHeight,只有Chrome需要使用body.scrollHeight。

同理

clientWidth、offsetWidth 和scrollWidth 的解释与上面相同,只是把高度换成宽度即可。

  1. /**
  2. * Figure out if current document is in quirks mode.
  3. * @param a: document object
  4. */
  5. function isQuirksMode(doc) {
  6. // In IE6,IE7,IE8,IE9,IE10 Firefox and Chrome browsers, document.compatMode has two values: "BackCompat", "CSS1Compat".
  7. return doc.compatMode && doc.compatMode.indexOf("CSS") != -1;
  8. }
  9. /**
  10. * Get the visible height of the whole document or window.
  11. */
  12. function getDocumentClientHeight(doc) {
  13. // in IE quirks mode, documentElement.clientHeight == 0
  14. return isQuirksMode(a) ? doc.body.clientHeight : doc.documentElement.clientHeight;
  15. }
  16. /**
  17. * Get the page height, if content's height is smaller than window's height, use window's height.
  18. */
  19. function getPageHeight(doc) {
  20. // for IE and Firefox, use documentElement.scrollHeight, Chrome use body.scrollHeight.
  21. return Math.max(doc.documentElement.scrollHeight, doc.body.scrollHeight);
  22. }
 
 转载

offsetHeight, clientHeight与scrollHeight的区别的更多相关文章

  1. clientHeight ,offsetHeight,style.height,scrollHeight有区别与联系

    style.height 包括 元素的滚动条,不包括边框clientHeight 不包括元素的滚动条和边框,只有在没有元素的滚动条的情况下,style.height相等于clientHeightoff ...

  2. offsetTop,offsetHeight,clientHeight,scrollHeight,scrollTop区别

    这些高度相信很多同学都搞不清楚吧.这里我通过本地测试,发现了区别. 以聊天窗口为例. 元素(class='content')高度444px,其中上下padding分别是10px,margin为0.距离 ...

  3. 关于height、offsetheight、clientheight、scrollheight、innerheight、outerheight的区别

    二.也是平时经常用到的offsetheight 它返回的高度是内容高+padding+边框,但是注意哦,木有加margin哦,当然一般也木有啥需要把margin加进去的,以上代码为例,结果显示上图h2 ...

  4. 关于height、offsetheight、clientheight、scrollheight、innerheight、outerheight的区别一览

    平时,不管在pc端页面还是移动端页面,因为我们一般很少会设置某个块的的高度,但是呢,我有时候有需要取到这些高度以便于我们方便进行判断和下一步的编写.一般这个时候我都是直接的获取一个块的高度.heigh ...

  5. 各种高度的区别及height、clientHeight、scrollHeight、offsetHeight的区分

    1.height.clientHeight.scrollHeight.offsetHeight 我们来实现test中的onclick事件    function justAtest()    {    ...

  6. innerHeight,clientHeight,offsetHeight,scrollWidth等的区别和用法

    要理解这几个属性,首先要搞明白body,documentElement的区别 1.body是DOM对象里的body子节点,即<body>标签2.documentElement是整个节点树的 ...

  7. 彻底搞清楚DOM元素的height,offsetHeight,clientHeight,scrollHeight

    测试用例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. height、clientHeight、scrollHeight、offsetHeight区别

    转自 http://www.cnblogs.com/yuteng/articles/1894578.html 我们来实现test中的onclick事件    function justAtest()  ...

  9. 关于offsetTop offsetHeight clientHeight scrollHeight scrollTop的区别研究

    我是以chrome浏览器做的研究. 先看一段代码: <script> window.addEventListener('DOMContentLoaded',function(){ var ...

随机推荐

  1. 使用NSAssert()和NSParameterAssert调试程序

    NSAssert: NSAssert()只是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否属于Bug,满足条件返回真值,程序继续运行,如果返回假值,则抛出异 ...

  2. OC 类簇与复合

    OC 类簇与复合 类簇: 类簇是Foundation框架中广泛使用的设计模式.类簇将一些私有的.具体的子类组合在一个公共的.抽象的超类下面,以这种方法来组织类可以简化一个面向对象框架的公开架构,而又不 ...

  3. python之局部变量引用赋值前的结果

    通过正则表达式,实现加减 昨晚在做计算器的时候,被一个BUG搞懵比了.现在再看看,发现我好小白啊~~ #++- num = input("please input:") sa = ...

  4. ASP.NET获取真正的客户端IP地址的6种方法

    Request.ServerVariables("REMOTE_ADDR") 来取得客户端的IP地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的IP地址,而不是真 ...

  5. 权重最小生成树的思想与Kruskal算法

    晚上做携程的笔试题,附加题考到了权重最小生成树.OMG,就在开考之前,我还又看过一遍这内容,可因为时间太紧,也从来没有写过代码,就GG了.又吃了眼高手低的亏.这不,就好好总结一下,亡羊补牢. 权重最小 ...

  6. mysql安装及配置服务

    第一次安装mysql 1.本地环境:windows 7 -64,mysql版本5.5.28(mysql-5.5.28-winx64.msi) 2.双击mysql-5.5.28-winx64.msi,进 ...

  7. SQL Server性能调优系列

    这是关于SQL Server调优系列文章,以下内容基本涵盖我们日常中所写的查询运算的分解以及调优内容项,皆为原创........ 第一个基础模块注重基础内容的掌握,共分7篇文章完成,内容涵盖一系列基础 ...

  8. C++STL - 类模板

    类的成员变量,成员函数,成员类型,以及基类中如果包含参数化的类型,那么该类就是一个类模板   1.定义 template<typename 类型形参1, typename 类型形参2,...&g ...

  9. x01.os.19: linux 0.0

    linux 0.0 是一个丢失的版本,但赵炯老师又在 linux 0.11 的基础上,使它起死回生.www.oldlinux.org 有大量资源可供下载,值得一看. 1.要编译运行,首先需安装:sud ...

  10. 某墙尼妹,用个Response.Filter来解决StackExchange.Exceptional中google cdn的问题

    某墙墙了古古路,一些开源的东东里用了古古路CDN,比如Exceptional,Opserver ,导致服务要么慢要么用不了 必须要替换之 Exceptional就只要用Response.Filter替 ...