当前位置:网站首页>找到页面当前元素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
}
边栏推荐
猜你喜欢

Deep learning classification network -- Network in network

在uni-app中引入uView

Sumo tutorial Hello World

栈(线性结构)

Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice

神机百炼3.53-Kruskal

Common means of modeling: combination

Singleton mode compilation

Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken

The official zero foundation introduction jetpack compose Chinese course is coming!
随机推荐
Redis key value database [advanced]
LeetCode 47. Full arrangement II
Talking about MySQL database
State machine in BGP
Lucene Basics
让每一位开发者皆可使用机器学习技术
Detailed explanation of BGP message
Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
深入了解JUC并发(二)并发理论
神机百炼3.54-染色法判定二分图
Redis key value database [primary]
Zhuanzhuanben - LAN construction - Notes
External interrupts cannot be accessed. Just delete the code and restore it Record this unexpected bug
深入了解JUC并发(一)什么是JUC
The difference between session and cookies
How to use mitmproxy
Don't use the new WP collection. Don't use WordPress collection without update
Mock simulate the background return data with mockjs
Web components series (VIII) -- custom component style settings
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!