如何找出文档中最高的 z-index?
为了将包含透明文本图像的 div 设置为文档中的最高 z-index,我选择了数字 10,000,它解决了我的问题。
之前我曾猜过数字3,但没有效果。
那么,是否有更科学的方法来确定哪个 z-index 高于所有其他元素的 z-index?
我尝试在 Firebug 中查找此指标,但找不到。
In order to set a div containing a transparent text image as the highest z-index in my document, I picked the number 10,000 and it solved my problem.
Previously I had guessed with the number 3 but it had no effect.
So, is there a more scientific way of figuring out what z-index is higher than that of all of your other elements?
I tried looking for this metric in Firebug but couldn't find it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
没有默认属性或任何东西,但您可以编写一些 JavaScript 来循环所有元素并找出答案。 或者,如果您使用像 jQuery 这样的 DOM 管理库,您可以扩展它的方法(或者查明它是否已经支持它),以便它开始跟踪页面加载时的元素 z 索引,然后检索最高的 z 索引就变得微不足道了。指数。
There isn't a default property or anything, but you could write some javascript to loop through all elements and figure it out. Or if you use a DOM management library like jQuery, you could extend its methods (or find out if it supports it already) so that it starts tracking element z-indices from page load, and then it becomes trivial to retrieve the highest z-index.
上面的“ES6”版本比第一个解决方案效率低,因为它在整个数组上进行了多次冗余传递。 相反,请尝试:
理论上,在一个减少步骤中完成它会更快,但一些快速基准测试显示没有显着差异,并且代码更粗糙
The "ES6" version above is less efficient than the first solution because it does multiple redundant passes across the full array. Instead try:
In theory it would be even quicker to do it in one reduce step, but some quick benchmarking showed no significant difference, and the code is gnarlier
在我看来,解决这个问题的最好方法就是为不同类型的元素设置不同类型的
z-index
的约定。 然后,您将通过查看文档找到要使用的正确z-index
。The best way to solve this problem is, in my opinion, just to set yourself conventions for what kinds of
z-index
es are used for different kinds of elements. Then, you'll find the correctz-index
to use by looking back at your documentation.最近我不得不为一个项目这样做,我发现我从 @Philippe Gerber 的这里有一个很好的答案,以及 @flo 的很好的答案(已接受的答案)。
与上面引用的答案的主要区别是:
z-index
和任何内联z-index
样式都会被计算,并使用两者中较大的一个进行比较和计算。auto
、static
等)都将被忽略。这里是代码示例的 CodePen,但它也包含在此处。
I had to do this for a project recently, and I found that I benefitted a lot from @Philippe Gerber's great answer here, and @flo's great answer (the accepted answer).
The key differences to the answers referenced above are:
z-index
, and any inlinez-index
style are calculated, and use the larger of the two for comparison and calculation.auto
,static
, etc) are ignored.Here is a CodePen for the code example, but it's included here as well.
使用 jQuery:
如果没有提供任何元素,它会检查所有元素。
Using jQuery:
if no elements supplied, it checks all elements.
Array.reduce()
这是确定最顶层
z-index
使用Array.reduce()
:Array.reduce()
Here's another solution to determine the topmost
z-index
that usesArray.reduce()
:ShadowRoot 解决方案
我们绝不能忘记自定义元素和影子根内容。
ShadowRoot solutions
We must not forget about custom-elements and shadow-root content.
如果您希望显示具有最高 z 索引的所有元素的 ID:
用法:
结果:
If you're looking to show the IDs of all elements with the highest z indices:
Usage:
Result:
一个解决方案深受 @Rajkeshwar Prasad。
A solution highly inspired from the excellent idea of @Rajkeshwar Prasad .
在 NodeList 中查找最大 zIndex 的稳健解决方案
getCompulatedStyle
和style
对象isNaN("" ) === 假
Robust solution to find maximum zIndex in NodeList
getComputedStyle
andstyle
object provided by node itselfisNaN("") === false
短的
Short
使用
map
和filter
的非常简单的代码Very simple code using
map
andfilter
基于之前的答案:
经过一些修改的函数
原型
用法
Based on previous answers:
function with some modifications
Prototype
usage
在浏览了 StackOverflow 上的大量解决方案后,我发现它们都没有真正正常工作,并考虑了 zIndex 实际如何工作。 我编写了一个解决方案,也考虑了堆叠上下文。 你可以参考这个文章 了解堆叠上下文在 CSS 中的工作原理。
请注意,此解决方案考虑所有元素,而不仅仅是定位元素,因为它们可以在添加某个类后变得定位。 但是您只需添加对位置计算样式属性的检查即可轻松解决此问题。
After looking through a lot of solutions here on StackOverflow - I've seen that none of them actually works correctly and considers how is zIndex actually working. I have written a solution that also takes into consideration the stacking context. You can refer to this article to understand how stacking context works in CSS.
Note that this solution considers all elements, and not only positioned ones because they can become positioned after some class is added. But you can fix this easily by just adding a check for the position computed style property.
我发现当页面上的 z 索引动态更改时,提供的方法不起作用(当前方法仅获取原始设置的 z 索引)。
此函数还适用于动态添加的 z 索引:
I have found the provided methods don't work when there have been z-indices changed dynamically on the page (the current methods only grab the originally set z-indices).
This function also works with dynamically added z indices:
这是我的两行函数:
Here is my two-line function:
这里只是提供另一种方式。
如果你只是想知道一个元素是否在顶层,
您可以使用 document.elementFromPoint 函数,
返回 (x,y) 坐标处的顶部元素,
例如:
但是,这仅检查元素的中心是否不在
被其他元素覆盖,但不是整个元素。
Just provide another way here.
If you just want to know whether a element is on the top layer,
you can use the document.elementFromPoint function,
which return the top element at (x,y) coordinate
eg:
However, this only check whether the center of the element is not
covered by other element, but not the whole element.
为了清楚起见,从 abcoder 网站窃取一些代码:
Stealing some code from abcoder site for the sake of clarity:
您可以为特定元素类型(例如
)调用
findHighestZIndex
,如下所示:假设
findHighestZindex
函数的定义如下:You could call
findHighestZIndex
for a particular element type such as<div>
like this:assuming a
findHighestZindex
function that is defined like this:使用 ES6 更简洁的方法
Using ES6 a cleaner approach
我想添加我在用户脚本之一中使用的 ECMAScript 6 实现。 我使用这个来定义特定元素的
z-index
,以便它们始终显示为最高。在 JS 中,您还可以为您可能想要排除的元素设置某些属性或类名。 例如,考虑您的脚本在您希望显示为最高元素(例如弹出窗口)的元素上设置
data-highest
属性; 并考虑一个您无法控制的类名为yetHigher
的元素,该元素应该更高(例如自定义上下文菜单)。 我可以使用链接的:not
选择器排除这些元素。 请注意,:not([data-highest], .yetHigher)
是可能的,但 实验性,截至 2021 年 1 月仅具有有限的浏览器支持。下面五行可以运行多次,并通过查找重复更新变量
highestZIndex
找出当前的最高ZIndex 值与所有元素的所有其他计算的z 索引之间的最大值。过滤器
排除所有“auto”
值。I’d like to add my ECMAScript 6 implementation that I use in one of my UserScripts. I’m using this one to define the
z-index
of specific elements so that they always appear the highest.In JS, you can additionally set certain attributes or class names to elements that you may want to exclude. For instance, consider your script setting a
data-highest
attribute on an element that you want to appear as the highest element (e.g. a popup); and consider an element with class nameyetHigher
that you don’t control, which should be even higher (e.g. a custom context menu). I can exclude these elements with a chained:not
selector. Note that:not([data-highest], .yetHigher)
is possible, but experimental, and only has limited browser support as of January 2021.The lower five lines can run multiple times and update the variable
highestZIndex
repeatedly by finding out the maximum between the currenthighestZIndex
value and all the other computed z-indexes of all elements. Thefilter
excludes all the"auto"
values.我相信你所观察到的是巫毒。 如果无法访问您的完整样式表,我当然无法可靠地判断; 但我觉得这里真正发生的事情很可能是您忘记了只有定位元素才会受到
z-index
的影响。此外,
z-index
es 不会自动分配,仅在样式表中分配,这意味着没有其他z-index
ed 元素,z-index: 1;
将位于其他所有内容之上。I believe what you are observing is Voodoo. Without access to your complete style sheet I can of course not tell reliably; but it strikes me as likely that what really has happened here is that you have forgotten that only positioned elements are affected by
z-index
.Additionally,
z-index
es aren't assigned automatically, only in style sheets, which means that with no otherz-index
ed elements,z-index:1;
will be on top of everything else.我想你必须自己做这件事......
I guess you have to do this yourself ...