当前位置:网站首页>Find the highest value of the current element Z-index of the page
Find the highest value of the current element Z-index of the page
2022-07-02 06:22:00 【Can't be lacking, can't be excessive】
z-index Property to set the stacking order of elements . Elements with a higher stacking order are always in front of elements with a lower stacking order .
z-index Need and position Property can only take effect when used together .
Use a background
Why do I need to find the current element of the page z-index The highest value ?
When we use some shell components , Hope it appears at the top of the page , Cover the rest of the page , Do such interaction .
Which components need to find the highest of the current page z-index?
For example, the common tooltip dialog popup alert And so on .
I set a large number assigned to z-index No way? ?
scene 1: A page or a project may be developed by more than one person at the same time , You can set a big number to z-index Where you need to . When your colleagues develop , If he finds that his development content is covered by your development content , Just set a larger number to z-index.
scene 2: One dialog Continue to pop up a dialog, So you have to set it twice dialog Of z-index,
In the long term , It will bring a lot of inconvenience to the front-end development .
There are two solutions
- The front-end team forms a unified specification , Different components set different z-index Range , Different businesses use fixed components .
- Automatically get the current z-index The highest value , Then add one to it .
If it's the first way , How to form norms , There will be no further discussion here .
obtain z-index The highest value
- Put all of the entire page dom Get , And turn it into an array .
document.all It's actually a pseudo array , Need to use Array.from Change my
const allElement = Array.from(document.all);
- Use window.getComputedStyle() Method to get the name of the element z-index attribute , If you don't get it, just go straight to 0
Why use getComputedStyle?
getComputedStyle Not only can you get css Written z-index, You can also get the style in the line
const zIndexArray = []
allElement.forEach((item) => {
zIndexArray.push(Number(window.getComputedStyle(item, null).getPropertyValue("z-index"))
})
- In some way ( such as max Or sort ) find z-index Maximum value .
const maxZIndex = Math.max(...zIndexArray)
- Will be the biggest z-index Add 1 return .
return maxZIndex + 1
The combined code is
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
}

Someone on the current test website will z-index Set up a 1000000
Optimize it properly , We will all be 0 Of z-index Get rid of
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
}
边栏推荐
- Classic literature reading -- deformable Detr
- Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
- 线性dp(拆分篇)
- 从设计交付到开发,轻松畅快高效率!
- 利用NVIDIA GPU将Minecraft场景渲染成真实场景
- 注解和反射详解以及运用
- LeetCode 78. subset
- Spark overview
- 深入了解JUC并发(一)什么是JUC
- TensorRT的功能
猜你喜欢

The official zero foundation introduction jetpack compose Chinese course is coming!

阿里云MFA绑定Chrome浏览器

找到页面当前元素z-index最高的数值

Lucene Basics

步骤详解 | 助您轻松提交 Google Play 数据安全表单

穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!

Pbootcms collection and warehousing tutorial quick collection release

Monitoring uplink of VRRP

ROS create workspace

Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
随机推荐
TensorRT的功能
BGP routing optimization rules and notification principles
LeetCode 78. 子集
Classic literature reading -- deformable Detr
The Chinese word segmentation task is realized by using traditional methods (n-gram, HMM, etc.), neural network methods (CNN, LSTM, etc.) and pre training methods (Bert, etc.)
Pbootcms collection and warehousing tutorial quick collection release
In depth understanding of JUC concurrency (I) what is JUC
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
CUDA用户对象
Database learning summary 5
Redis---1. Data structure characteristics and operation
数据科学【九】:SVD(二)
CUDA中内置的Vector类型和变量
栈(线性结构)
I/o multiplexing & event driven yyds dry inventory
Spark overview
Introduce uview into uni app
I/o impressions from readers | prize collection winners list
Flutter hybrid development: develop a simple quick start framework | developers say · dtalk
程序员的自我修养—找工作反思篇