前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >解析offsetHeight,clientHeight,scrollHeight之间的区别「建议收藏」

解析offsetHeight,clientHeight,scrollHeight之间的区别「建议收藏」

作者头像
全栈程序员站长
发布2022-09-30 20:24:57
4620
发布2022-09-30 20:24:57
举报

大家好,又见面了,我是你们的朋友全栈君。

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

clientHeight

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

offsetHeight 在IE6,IE7,IE8以及最新的的FF, Chrome中,在元素上都是offsetHeight = clientHeight + 滚动条 + 边框。

scrollHeight scrollHeight是元素的padding加元素内容的高度。这个高度与滚动条无关,是内容的实际高度。

计算方式 :scrollHeight = topPadding + bottomPadding + 内容margix box的高度。

在浏览器中的区别在于:

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

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

注: 以上都是对于一般元素而方言的,body和documentElement的clientHeight, offsetHeight和scrollHeight在各个浏览器中的计算方式又不同。在所有的浏览器中,如果你想获取整个视窗的高度,你得用documentElement.clientHeight,因为body.clientHeight是由它的内容决定的。 关于body和documentElement的这些属性,则完全是另外一回事:

FF19

在body上设置overflow:scroll就是设置浏览器的滚动条,导致body.clientHeight永远都等于body.scrollHeight。

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

offsetHeight:clientHeight+ body.border;

scrollHeight== clientHeight。

documentElement clientHeight= window视窗高度 -scrollbar.width。

offsetHeight= body.offsetHeight + body.margin。

scrollHeight= 内容的高度(与body的height样式无关),但最小值是documentElement.clientHeight。

元素上 offsetHeight= padding + border + height。

clientHeight= padding + height – scrollbar.width。

scrollHeight>= clientHeight

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

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

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

offsetHeight = body.clientHeight + body.border;

scrollHeight= body.padding + 内容的高度(与height样式无关),但最小值是documentElement.clientHeight。

documentElement clientHeight= window视窗高度 – 滚动条高度。

offsetHeight = body.offsetHeight + body.margin;

scrollHeight = 内容的高度(与body上的height无关),但最小值是documentElement.offsetHeight。

元素上 offsetHeight = padding + border + height。

clientHeight = padding + height – scrollbar.width。

scrollHeight >= clientHeight

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

offsetHeight= clientHeight + border;

scrollHeight= body.padding + 内容的高度(与height样式无关),但最小值是documentElement.clientHeight。

documentElement clientHeight = window窗口大小 – 滚动条大小

offsetHeight = body.offsetHeight + body.margin

scrollHeight= 内容的高度(与body上的height无关),但最小值是documentElement.offsetHeight。

IE8 在IE8下,滚动条的大小是17个像素。

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

offsetHeight = clientHeight + body.border

scrollHeight =内容的高度(与body上的height无关),但最小值是clientHeight。

documentElement clientHeight = 窗口大小(除去滚动条后)

offsetHeight = clientHeight + 滚动条大小 + body.border

scrollHeight=内容的高度(与body上的height无关),但最小值是body.offsetHeight+ margin。

元素上 offsetHeight = padding + border + height。

clientHeight = padding + height – scrollbar.width。

scrollHeight >= clientHeight

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

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

IE7 在IE7中,body上设置的滚动条并不是窗口的滚动条,这个需要注意。

body clientHeight= body.padding + height(css设置或内容撑大)– body上的滚动条。

offsetHeight= clientHeight + 滚动条的大小+body.border。

scrollHeight= 内容的高度(与body上的height无关)。

documentElement clientHeight = window视窗大小(与滚动条的有无无关)

offsetHeight = clientHeight。

scrollHeight = body.offsetHeight + border.margin

元素 offsetHeight = padding + border + height。

clientHeight = padding + height – scrollbar.width。

scrollHeight = padding + 内容marginbox的高度

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

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

IE6 在IE6中,与IE7类似, body上设置的滚动条并不是窗口的滚动条。

body clientHeight = body.padding + body.height

offsetHeight = body.clientHeight + body.border + body上滚动条大小。

scrollHeight =内容的高度(与body上的height无关)。

documentElement 在IE6中,与IE7类似,虽然body上设置的滚动条并不是窗口的滚动条,但它的clientHeight和offsetHeight还算与其它浏览器想统一。

clientHeight = 窗口大小(除去窗口滚动条大小后)

offsetHeight =clientHeight + body.border

scrollHeight = body.offsetHeight + body.margin

元素 offsetHeight = padding + border + height。

clientHeight = padding + height – scrollbar.width。

scrollHeight = padding + 内容margin box的高度

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

同理

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/195283.html原文链接:https://javaforall.cn

本文参与?腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月10日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体同步曝光计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com