前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >offsetheight和clientheight_scrollheight属性

offsetheight和clientheight_scrollheight属性

作者头像
全栈程序员站长
发布2022-10-03 15:47:12
4670
发布2022-10-03 15:47:12
举报

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

介绍

网上介绍clientheight、offsetheight、scrollheight的帖子很多,看后感觉明白了,一细想似乎又不明白了。为了获取更权威的解答,查阅了MDN 文档,希望能帮助后来人。为了加深理解,看后,最好做下后边的实验。

clientheight

clientheight,内容的可视区域,不包含border。clientheight=padding+height-横向滚动轴高度。

The Element.clientHeight read-only property is zero for elements with no CSS or inline layout boxes, otherwise it’s the inner height of an element in pixels, including padding but not the horizontal scrollbar height, border, or margin.

这里写图片描述
这里写图片描述

offsetheight

offsetheight,它包含padding、border、横向滚动轴高度。 offsetheight=padding+height+border+横向滚动轴高度

The HTMLElement.offsetHeight read-only property is the height of the element including vertical padding and borders, as an integer. Typically, an element’s offsetHeight is a measurement in pixels of the element’s CSS height, including border, padding and the element’s horizontal scrollbar (if present, if rendered). For the document body object, the measurement includes total linear content height instead of the element’s CSS height. Floated elements extending below other linear content are ignored.

这里写图片描述
这里写图片描述

scrollheight

scrollheight,可滚动高度,就是将滚动框拉直,不再滚动的高度,这个很好理解。scrollHeight通常用在iframe自适应内容高度的情况。

The Element.scrollHeight read-only property is a measurement of the height of an element’s content, including content not visible on the screen due to overflow. The scrollHeight value is equal to the minimum height the element would require in order to fit all the content in the viewpoint without using a vertical scrollbar. It includes the element’s padding, but not its border or margin.

这里写图片描述
这里写图片描述

试验

现在我们做试验,来验证上述公式。 我们定义了一个滚动div和非滚动div,这两个div高度、宽度、padding、margin、border都一样,区别就是横向滚动不一样,然后分别,打印出clientHeight、offsetHeight、scrollHeight 在线演示地址

代码语言:javascript
复制
#scrolldiv{ width:150px; height:150px; display:block; overflow-y:auto; padding:5px; border: solid red 2px; margin:30px; box-sizing: content-box; }

效果图

这里写图片描述
这里写图片描述

验证clientHeight

根据公式,clientHeight=height+padding-横向滚动轴高度,143=150+(5+5)-横向滚动轴高度,得出横向滚动轴高度是17。为了看一下横向滚动轴高度是否是17,看一下第二个没有滚动轴的div的clientHeight正好是160,160-143=17,所以,我们可以肯定横向滚动轴高度是17,并且公式是正确的。

这是我们反推出来的横向滚动轴高度是17,根据浏览器的渲染模型也能正向推算出clientHeight,如图

这里写图片描述
这里写图片描述

可见内容可视高度是133,padding是5,所以clientHeight=渲染后的高度+padding=133+(5+5)=143,也符合预期。

验证offsetHeight

根据公式,offsetheight=padding+height+border+横向滚动轴高度,得出(5+5)+133+(2+2)+17=164,跟显示出来的高度一样。

scrollHeight这里就不验证了。

这张是没有横向滚动轴的渲染图

这里写图片描述
这里写图片描述

结论

通过上述实验,我们能得出,实际的可视区域比设置的值要小,因为滚动轴占去一部分。在计算时,clientHeight会剔除滚动轴,而offsetHeight会包含滚动轴。记住这两个公式

代码语言:javascript
复制
clientHeight=height+padding-横向滚动轴高度
offsetheight=padding+height+border+横向滚动轴高度

通过做实验,彻底理解了clientHeight、offsetHeight,实践出真知呀!

注意

在做测试时,一定要将css样式放在javascript之前或者window加载完毕后执行js,否则解析结果跟预期不同。

参考

https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetHeight https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight

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

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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
    • clientheight
      • offsetheight
        • scrollheight
        • 试验
          • 验证clientHeight
            • 验证offsetHeight
            • 结论
            • 注意
            • 参考
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
            http://www.vxiaotou.com