CSS/JavaScript:使元素成为最顶层的 z-index/最顶层的模态元素
我想让一个元素(例如
)成为页面上的最顶层。我的假设是,我可以做到这一点的唯一方法是指定元素具有 style="z-index:"
值,该值是浏览器允许的最大值(int32?)。
这是正确的吗?
相反,是否有可能以某种方式获取最高元素的 z-index
,并使该
的 z-index
> [最高元素的值] + 1
?例如:$myDiv.css("z-index", $(document.body).highestZIndex() + 1);
模态 JavaScript“窗口”如何工作?
I would like to make an element (e.g. a <div>
) be the top-most layer on the page.
My assumption is that the only way I can do this is to specify that the element has a style="z-index:"
value that is the maximum the browser allows (int32?).
Is this correct?
Instead, would it be possible to somehow get the element's z-index
whose is highest, and make this <div>
's z-index
the [highest element's value] + 1
? For example:
$myDiv.css("z-index", $(document.body).highestZIndex() + 1);
How do modal JavaScript "windows" work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
http://abcoder. com/javascript/a-better-process-to-find-maximum-z-index-within-a-page/ ->找到最大 z-index 并为其分配 +1。
http://abcoder.com/javascript/a-better-process-to-find-maximum-z-index-within-a-page/ -> find the max z-index and assign +1 to it.
Sheavi 的 jQuery 解决方案不起作用,因为 z-index 是 css 样式,而不是属性。
请尝试这样做:
由于 Javascript 的异步特性,返回值可能不是您所期望的,但在任何元素上调用该函数都可以正常工作。
Sheavi's jQuery solution doesn't work because z-index is a css style, not an attribute.
Try this instead:
Return value may not be what you expect due to Javascript's async nature, but calling the function on any element will work fine.
操作方法如下:
highest_index 现在包含页面上最高的 z-index...只需向该值添加 1 并将其应用到您想要的任何位置。您可以像这样应用它:
这是使用 jQuery 实现相同目标的另一种方法:
同样,将新索引应用到元素的方法相同:
Here's how to do it :
highest_index now contains the highest z-index on the page... just add 1 to that value and apply it wherever you want. You can apply it like so :
Here's another way of achieving the same thing using jQuery :
Again, same way to apply the new index to an element :
堆叠上下文怎么样? 并不总是这样:在文档上,最高的 z-index 将位于顶部。请参阅:http://philipwalton.com/articles/没有人告诉你关于 z-index 的事情/。如果不考虑堆叠上下文,设置 10 亿可能不足以使您的元素位于最顶层。
What about stacking context? It is not always true that: On a document highest z-index will be on top. See: http://philipwalton.com/articles/what-no-one-told-you-about-z-index/. If you do not take stacking context into account, setting a billion may not be enough to make your element on the top-most.