前言

很多年前有记入过一篇 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)的更多相关文章

  1. JavaScript中的 offset, client,scroll

    在js 中我们要用到的 offset, client, scroll 在这我把自己理解的给大家分享一下. offset div.offsetTop 指div距离上方或上层控件的距离,单位像素 div. ...

  2. offset/client/scroll一些总结

    offset/client/scroll一些总结 1.offset 首先offset共有五个值 1.offsetParent 2.offsetTop 3.offsetLeft 4.offsetWidt ...

  3. js中 offset /client /scroll总结

    offset家族(只能读取,不能操作): offsetLeft:元素的边框的外边缘距离与已定位的父容器(offsetparent)的左边距离(就是子元素左边框到父元素左边框的距离). offsetTo ...

  4. js 元素offset,client , scroll 三大系列总结

    1,element.offsetWidth : 包括 padding 和 边框 2,element.clientWidth : 包括 padding ,不包含边框 , 内容超出会溢出盒子的时候,就用s ...

  5. offset client scroll

    offsetHeight offsetWidth返回为元素在屏幕上显示大小,不包括外边距 clientHeight clientWidht 和上面两个类似,不同的是,这两个不包括外边距高度. < ...

  6. js中常用的offset client screen对象

    javascript中offsetWidth.clientWidth.width.scrollWidth.clientX.screenX.offsetX.pageX offsetWidth //返回元 ...

  7. scroll offset & client总结

    oEvent.clientX 是指鼠标到可视区左边框的距离. oEvent.clientY 是指鼠标到可视区上边框的距离. clientWidth  是指可视区的宽度. clientHeight  是 ...

  8. Html 中scroll offset client 总结

    HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...

  9. bom中的offset,client,scroll

    简单明了

  10. python 全栈开发,Day52(关于DOM操作的相关案例,JS中的面向对象,定时器,BOM,client、offset、scroll系列)

    昨日作业讲解: 京东购物车 京东购物车效果: 实现原理: 用2个盒子,就可以完整效果. 先让上面的小盒子向下移动1px,此时就出现了压盖效果.小盒子设置z-index压盖大盒子,将小盒子的下边框去掉, ...

随机推荐

  1. mysql Using join buffer (Block Nested Loop) join连接查询优化

    最近在优化链表查询的时候发现就算链接的表里面不到1w的数据链接查询也需要10多秒,这个速度简直不能忍受 通过EXPLAIN发现,extra中有数据是Using join buffer (Block N ...

  2. [oeasy]python0012_字符_character_chr函数_根据序号得到字符

    ​ 字符(character) 回忆上次内容 上次了解了ord函数 这个函数可以通过字符得到序号 那么可以反过来吗? 通过序号得到字符可以吗? ​ 编辑 ord的逆运算chr 有来就有回 ​ 编辑 好 ...

  3. 解决“网页源代码编码形式为utf-8,但爬虫代码设置为decode('utf-8')仍出现汉字乱码”的问题

    为了用爬虫获取百度首页的源代码,检查了百度的源代码,显示编码格式为utf-8 但这样写代码,却失败了-.. (这里提示:不要直接复制百度的URL,应该是http,不是https!!!) # 获取百度首 ...

  4. C# 12 新增功能实操!

    前言 今天咱们一起来探索并实践 C# 12 引入的全新功能! C#/.NET该如何自学入门? 注意:使用这些功能需要使用最新的 Visual Studio 2022 版本或安装 .NET 8 SDK ...

  5. Scratch全套Q版三国人物角色素材包免费下载

    全新Q版三国人物角色素材包,内含142张细腻可爱的Q版风格图片,涵盖三国名将.士兵.场景等丰富元素,为scratch爱好者提供多样选择,适合各类三国主题创作. 免费下载:www.xiaohujing. ...

  6. windows上传app到构建版本的方法

    ios打包好ipa文件后,ipa文件需要上架到app store,用户才能安装.而在app store里,无法直接将ipa上传,需要使用工具上传,但是官方提供的工具,比如xcode等只能安装在苹果电脑 ...

  7. HIC simple process

    1,什么是Hic数据? Hi-C是研究染色质三维结构的一种方法.Hi-C技术源于染色体构象捕获(Chromosome Conformation Capture, 3C)技术,利用高通量测序技术,结合生 ...

  8. NVIDIA机器人仿真项目 —— Isaac Gym - Preview Release

    地址: https://developer.nvidia.com/isaac-gym 过期代码库地址:(已不再维护的代码库,现已由isaac gym项目合并到isaac sim项目)(2022年开始停 ...

  9. python编程中的circular import问题

    循环引入,circular import是编程语言中常见的问题,在C语言中我们可以使用宏定义来处理,在c++语言中我们可以使用宏定义和类的预定义等方式来解决,那么在python编程中呢? 其实在pyt ...

  10. springboot接口入参下划线转驼峰以及返回参数驼峰转下划线实现

    1.背景 在实际开发中,通常来说java里面是使用驼峰的命名规则; 但是有时候在对接其他三方平台的接口时,要求使用下划线的命名规则,这时候就涉及到如何让自己的接口满足三方平台的下划线; 实现方式有 1 ...