DOM – Dimension & Coordinate (offset, client, computed, rect)
前言
很多年前有记入过一篇 box-sizing 和 dom width.
想想也挺可悲的, 那年我是负责后端的, 却要帮着前端去学习这些知识来解决问题...
也好, 现在 full stack, 也算没有浪费当年的努力. 这篇是翻新版本.
和 Dimension 相关的属性
我这里以 div height 作为例子. 你把它们都换成 width 理解也是一样的.
offset-height
client-height
height (getComputedStyle)
scroll-height
rect height (getBoundingClientRect)
以上就是常见的高度
影响 dimension 的属性
它们会被
box-sizing
border
padding
scale
有没有 scollbar 影响
但不受 margin, outline 影响 (许多人误以为会)
各个属性计算
offset-height
它是 border to border
border, padding, scrollbar, content 全算在内,除了 margin

红色是 border, 粉红色是 padding, 右边红线表示它计算的范围
client-height
它是 padding to padding 扣掉 scrollbar
padding, content 算,margin, border, scrollbar 不算。

scrollbar 一般上是在 padding 里面 (重叠的),如果没有 padding 或者 padding size 小于 scrollbar,那 content 会被挤压。
height (getComputedStyle)
height 的计算 depend on box-sizing
border-box 情况下, height 包括 border 和 padding.

content-box 情况下, heigth 不包括 padding 和 border (当然也不包括 scrollbar)

scroll-height (对标 client-height)
在 hug content 的情况下, scroll-height 和 client-height 始终是一致的.
只有在 overflow-y: auto 的情况下才会使用到 scroll-height. (注意: visiable 情况下 scroll-height 的计算和 auto 是不同的哦, 我也没有搞懂, 但一般场景都是 auto 嘛, 以后再研究呗)
当 overflow-y 的时候, client-height 就变小了.

这个时候通过 scroll-height 就可以拿到原本的 client-height 的尺寸.
容易混淆的情况:
假设出现了 horizontal scrollbar, 请问 scroll-height 和 client-height 一样吗?
答案是 yes. 因为那是 horizontal scrollbar, 仅代表 width 的内容超过了, height 并没有, 而 scroll-height 是看有没有 vertical scrollbar 的出现.
同时 client-height 会少算 horizontal scrollbar 同样的 scroll-height 也会少算 horizontal scrollbar.
rect height (getBoundingClientRect) (对标 offset-height)
在没有 scale 的情况下, 它和 offset-height 始终保持一致.
有 scale 的情况下, 它获取的是 scale 之后的高度. 比如 offset-height 是 100px, scale 1.5
那么 reat height 就是 100 x 1.5 = 150px
另外, rect height 比 offset-height 精准,因为 rect height 有小数点,而 offset-height 是 int 它会自动 rounding。
也可以参考这里
经典图片

MouseEvent 的 Coordinate
mouse event 里有 4 种 coordinate info.
分别是 page, screen, client, offset
场景说明:

2 个 section 2 个 box
offset coordinate
event.offsetX, event.offsetY

event listening 的 target element 和 mouse click 点之间的距离 (AKA target relative)
注意 parent child 冒泡的情况

假设 parent pink color div 有 mouseenter event
当 mouse 进入 parent pink color div。它的 offset 对标的是 parent div。上图的第一个红点。
当 mouse 进入 child blue color div。它的 offset 对标换成了 child div,不再是 parent div 了。
虽然 event 只绑定在 parent div,但是 offset x,y 认的是最靠近的 offset element。
另外一点需要注意,当有 transform 的时候

虽然 img 有 translate 了,但是 mouse click 图片左上角依然拿到 0,0

img scale(2),虽然 mouse click 的距离 actual 是 220,80 但是获取到的是 110, 40,因为放大了一倍,所以少了一半。
这就是它的效果。注意咯
screen coordinate
event.screenX, event.screenY

mouse click 点和 screen 的距离. 注意哦, 我的 browser 是缩小的, 它依然是计算到最外面 monitor screen 的距离. (AKA screen relative)
client coordinate
event.clientX, event.clientY

clientX, Y 是最常被使用到的. 它和 getBoundingClientRect x,y 一样. (AKA window relative)
page coordinate

page 和 client 类似, 只是它有算 scroll, 所以会比 client 多. (AKA document relative)
DOM – Dimension & Coordinate (offset, client, computed, rect)的更多相关文章
- JavaScript中的 offset, client,scroll
在js 中我们要用到的 offset, client, scroll 在这我把自己理解的给大家分享一下. offset div.offsetTop 指div距离上方或上层控件的距离,单位像素 div. ...
- offset/client/scroll一些总结
offset/client/scroll一些总结 1.offset 首先offset共有五个值 1.offsetParent 2.offsetTop 3.offsetLeft 4.offsetWidt ...
- js中 offset /client /scroll总结
offset家族(只能读取,不能操作): offsetLeft:元素的边框的外边缘距离与已定位的父容器(offsetparent)的左边距离(就是子元素左边框到父元素左边框的距离). offsetTo ...
- js 元素offset,client , scroll 三大系列总结
1,element.offsetWidth : 包括 padding 和 边框 2,element.clientWidth : 包括 padding ,不包含边框 , 内容超出会溢出盒子的时候,就用s ...
- offset client scroll
offsetHeight offsetWidth返回为元素在屏幕上显示大小,不包括外边距 clientHeight clientWidht 和上面两个类似,不同的是,这两个不包括外边距高度. < ...
- js中常用的offset client screen对象
javascript中offsetWidth.clientWidth.width.scrollWidth.clientX.screenX.offsetX.pageX offsetWidth //返回元 ...
- scroll offset & client总结
oEvent.clientX 是指鼠标到可视区左边框的距离. oEvent.clientY 是指鼠标到可视区上边框的距离. clientWidth 是指可视区的宽度. clientHeight 是 ...
- Html 中scroll offset client 总结
HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...
- bom中的offset,client,scroll
简单明了
- python 全栈开发,Day52(关于DOM操作的相关案例,JS中的面向对象,定时器,BOM,client、offset、scroll系列)
昨日作业讲解: 京东购物车 京东购物车效果: 实现原理: 用2个盒子,就可以完整效果. 先让上面的小盒子向下移动1px,此时就出现了压盖效果.小盒子设置z-index压盖大盒子,将小盒子的下边框去掉, ...
随机推荐
- UE4 WebUI使用指南2-通信
前面一篇WebUI的文章讲述的WebUI插件的下载,开启,在UE中创建,加载网页等. 本文继续讲述通过WebUI,UE和网页实现双向通信的实现思路. 一点说明 由于WebUI 使用的浏览器内核并不是最 ...
- 安装PHP拓展
win环境下: php扩展下载地址:http://pecl.php.net/ 需要知道: php版本,操作系统位数,线程是否安全.想要知道这3个,在php中输入.如下图所示:phpinfo();di ...
- TIER 0: Meow
TIER 0: Meow Virtual Machine 虚拟机 (VM) 指通过软件模拟完整计算机系统 可以实现环境隔离 模型宿主机不具备的架构 虚拟机的工作原理:在物理机器的操作系统上,以一个应用 ...
- 解锁 SQL Server 2022的时间序列数据功能
解锁 SQL Server 2022的时间序列数据功能 SQL Server2022在处理时间序列数据时,SQL Server 提供了一些优化和功能,比如 DATE_BUCKET 函数.窗口函数(如 ...
- AMD 驱动安装 error 192解决问题
AMD 驱动安装 error 192解决问题 环境:win10专业版.自动更新已禁止.硬件安装设置[是](window自动下载驱动) [以下环境均在安全模式下进行 (win+r msconfig,安全 ...
- SptingBoot同时接收文件和对象数据(接收表单数据)
emmm...虽然很简单,而且网上全是教程,但是自己做个笔记映像才会更深刻,还请各位前辈多多指教: @RequestMapping(value = "/eventPush", me ...
- 【Java】Jsoup 解析HTML报告
一.需求背景 有好几种报告文件,目前是人肉找报告信息填到Excel上生成统计信息 跟用户交流了下需求和提供的几个文件,发现都是html文件 其实所谓的报告的文件,就是一些本地可打开的静态资源,里面也有 ...
- 人形机器人(humanoid)(双足机器人、四足机器人)—— 操控员 —— 机器人数据收集操作员
参考: https://www.youtube.com/watch?v=jbQ4M4SNb2M 机器人数据收集操控员,就和大模型训练数据收集员.数据类型标识员(打标签人员)一样,都是为了人工生成AI训 ...
- MPI4PY的数据类型 —— MPI4Py data type
原文地址: http://education.molssi.org/parallel-programming/03-distributed-examples-mpi4py/index.html MPI ...
- 【转载】手动DIY制作机械臂
相关链接: https://news.cnblogs.com/n/703664/ https://www.bilibili.com/video/BV12341117rG https://www.cnb ...