当前位置:网站首页>找到页面当前元素z-index最高的数值
找到页面当前元素z-index最高的数值
2022-07-02 06:10:00 【可缺不可滥】
z-index 属性设置元素的堆叠顺序。拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元素的前面。
z-index 需要与position属性配合使用才可以生效。
使用背景
为什么需要找到页面当前元素z-index最高的数值?
我们在使用某些弹层组件的时候,希望它出现在页面的最顶层,盖住页面的其它内容,做这样的交互。
哪些组件需要找到当前页面的最高z-index?
比如常见的tooltip dialog popup alert等组件。
我设置一个很大的数字赋值给z-index不行么?
情景1: 一个页面或者一个项目可能不只一个人同时开发,你可以设置一个很大的number给z-index在你需要的地方。当你同事开发的时候,如果他发现他的开发内容被你的开发内容遮住时,就只有设置一个更大的数字给z-index。
情景2:一个dialog里面继续弹出一个dialog,这样你就得分别设置两次dialog的z-index,
长此以往,会给前端开发带来许多不便。
解决的办法有两种
- 前端团队形成统一规范,不同的组件设置不同的z-index范围,不同的业务使用固定的组件。
- 自动获取当前z-index最高的值,然后在它基础上加一。
如果是第一种方式,如何形成规范,这里不再继续讨论。
获取z-index最高的值
- 将整个页面的所有dom获取到,并转成数组。
document.all 实际上是一个伪数组,需要使用Array.from转换一下
const allElement = Array.from(document.all);
- 使用window.getComputedStyle()方法获取元素的z-index属性,如果没有获取到就直接为0
为什么使用 getComputedStyle?
getComputedStyle不仅能获取css写的z-index,还可以获取行内的样式
const zIndexArray = []
allElement.forEach((item) => {
zIndexArray.push(Number(window.getComputedStyle(item, null).getPropertyValue("z-index"))
})
- 通过某种方式 (比如max或者排序)找到z-index最大的值。
const maxZIndex = Math.max(...zIndexArray)
- 将最大的z-index加1返回。
return maxZIndex + 1
合起来的代码就是
function getMaxZIndex() {
const allElement = Array.from(document.all);
const zIndexArray = []
allElement.forEach((item) => {
zIndexArray.push(Number(window.getComputedStyle(item, null).getPropertyValue("z-index")) || 0)
})
const maxZIndex = Math.max(...zIndexArray)
return maxZIndex + 1
}
当前测试网站有人将z-index设置成了1000000
再适当优化一下,我们将所有为0的z-index去掉
function getMaxZIndex() {
const allElement = Array.from(document.all);
const zIndexArray = []
allElement.forEach((item) => {
const itemZIndex = Number(window.getComputedStyle(item, null).getPropertyValue("z-index"))
if(itemZIndex) {
zIndexArray.push(itemZIndex)
}
})
let maxZIndex = 0
if(zIndexArray.length) {
maxZIndex = Math.max(...zIndexArray)
}
return maxZIndex + 1
}
边栏推荐
- LeetCode 27. 移除元素
- Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
- [C language] simple implementation of mine sweeping game
- BGP 路由優選規則和通告原則
- 从设计交付到开发,轻松畅快高效率!
- Data playback partner rviz+plotjuggler
- Generic classes and parameterized classes of SystemVerilog
- Picture clipping plug-in cropper js
- Arduino Wire 库使用
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
猜你喜欢
Current situation analysis of Devops and noops
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
神机百炼3.53-Kruskal
深入学习JVM底层(二):HotSpot虚拟机对象
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
ES6的详细注解
Introduce uview into uni app
Redis Key-Value数据库【初级】
穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
State machine in BGP
随机推荐
51 single chip microcomputer - ADC explanation (a/d conversion, d/a conversion)
No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
BGP报文详细解释
Common means of modeling: combination
Stc8h8k series assembly and C51 actual combat - digital display ADC, key serial port reply key number and ADC value
I/o impressions from readers | prize collection winners list
队列(线性结构)
LeetCode 47. 全排列 II
加密压缩文件解密技巧
社区说|Kotlin Flow 的原理与设计哲学
Detailed explanation of BGP message
Linear DP (split)
Monitoring uplink of VRRP
Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
Keepalived installation, use and quick start
深入学习JVM底层(三):垃圾回收器与内存分配策略
LeetCode 47. Full arrangement II
Comment utiliser mitmproxy
Scheme and implementation of automatic renewal of token expiration
How to use mitmproxy