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压盖大盒子,将小盒子的下边框去掉, ...
随机推荐
- 【实操记录】MySQL主从配置
本文使用MySQL原生支持的主从同步机制,详细记录了配置步骤及运维操作方法,可供大家直接参考.使用. 本文假设已经部署了两台主机的MySQL软件,且数据库服务正常,详细部署步骤可本站搜索:" ...
- git fetch origin
可以运行 git fetch origin 来同步远程服务器上的数据到本地.该命令首先找到 origin 是哪个服务器(本例为 git.ourcompany.com),从上面获取你尚未拥有的数据,更新 ...
- oeasy教您玩转python - 008 - # ascii码表
ASCII 码表 回忆上次内容 通过 help()可以从 python 命令行模式进入到帮助模式 通过 q 退出 ord(c)和 chr(i) 这是俩函数 这俩是一对,相反相成的 ord 通过字符 ...
- Java基础 韩顺平老师的 集合 的部分笔记
498,集合介绍 499,集合体系图(两个图背下) package com.hspedu.collection; import java.util.ArrayList; import java.uti ...
- AT_abc246_d 题解
洛谷链接&Atcoder 链接 本篇题解为此题较简单做法及较少码量,并且码风优良,请放心阅读. 题目简述 给定整数 \(N\),请你找到最小的整数 \(X\),满足: \(X \ge N\). ...
- .NET单元测试使用AutoFixture按需填充属性的几种方式,以及最佳实践
AutoFixture是一个.NET库,旨在简化单元测试中的数据设置过程.通过自动生成测试数据,它帮助开发者减少测试代码的编写量,使得单元测试更加简洁.易读和易维护.AutoFixture可以用于任何 ...
- Umov移动方块-scratch编程作品
程序说明: <Umov移动方块>是一款基于Scratch平台制作的小游戏.在这个游戏中,玩家将面对一个3×3的圆圈棋盘,并通过鼠标控制蓝色方块在这些圆圈中灵活移动.游戏的挑战在于,舞台的四 ...
- 【JDBC】自定义事务注解实现
参考自: https://blog.csdn.net/qq_28986619/article/details/94451889 数据源选型,我采用的是C3P0,下面是需要的依赖: <?xml v ...
- 【Spring-Security】Re04 Matchers配置规则API
一.使用antMatchers放行静态资源: package cn.zeal4j.configuration; import cn.zeal4j.handler.FarsAuthenticationF ...
- 【TypeScript】01 基础入门
前提:使用TypeScript你需要安装NodeJS支持 然后安装TypeScript: npm intsall -g typescript 安装完成后查看版本号: tsc -v 新建一个TypeSc ...