孩子和父母之间的 z-index
我在计算我们正在开发的应用程序的 z-index 顺序时遇到问题,我有两个根父级、一个导航栏和一张地图,以及一个子级,即地图工具提示。导航栏应该在地图上方可见,因此它具有更高的 z-index,但问题是使地图容器中的工具提示也显示在侧边栏上,有点难以解释,因此您可以可视化案例 http://jsbin.com/afakak/2/edit#javascript,html ,live:
<div id="nav-bar">
The nav bar
</div>
<div id="map-container">
This is the map container
<div id="tooltip">
This is the Tooltip
</div>
</div>
感谢您的任何 帮助。
I'm having problems working out the z-index order for an application we're working on, i have two root parents, a nav bar and a map, and one child, the map tooltip. The navbar should be visible above the map, so it has a higher z-index, but the problems is to make the tooltip in the map container to be displayed over the sidebar as well, a bit hard to explain, so you can visualize the case on http://jsbin.com/afakak/2/edit#javascript,html,live :
<div id="nav-bar">
The nav bar
</div>
<div id="map-container">
This is the map container
<div id="tooltip">
This is the Tooltip
</div>
</div>
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果
#map-container
已定位(即不是静态),则这是不可能的,因为 z-index 的比较方式:body
(或任何其他定位的父元素) ) 是#map-container
和#nav-bar
的参考。您给它们的任何z-index
都是根据父元素计算的。因此,具有较高 z-index 的 2 个元素之一将渲染在另一个元素及其所有子元素之上。#tooltip
的 Z-index 只会与#map-container
的其他子级进行比较。您可以按照 Nacho 所说的那样静态定位
#map-container
。如果您愿意,您可以通过 Javascript 模拟固定定位。如果您无法做到这一点,则需要更改标记,以便
#nav-bar
和#tooltip
具有共同的定位父元素。将#nav-bar
移至#map-container
内,或将#tooltip
移出其中。If
#map-container
is positioned (i.e. not static), this is not possible, because of the way z-index is compared:body
(or any other positioned parent element) is the reference for both#map-container
and#nav-bar
. Anyz-index
you give them is calculated in respect to the parent element. So the one of the 2 elements with the higher z-index will be rendered above the other one and all its child elements. Z-index of#tooltip
will only be compared with other children of#map-container
.You could do as Nacho said and statically position
#map-container
. You can simulate fixed positioning via Javascript, if you like.If you cannot do that, you need to change your markup, so that
#nav-bar
and#tooltip
have a common positioned parent element. Either move#nav-bar
inside#map-container
, or#tooltip
out of it.下面的解决方案应该有效,但我不知道您是否有将
nav-bar
保留在map-container
之外的要求。如果是这样,我认为没有解决方法。CSS:
HTML:
Below solution should work but I don't know if you have a requirement like keeping
nav-bar
outsidemap-container
. If so I don't think that there is a workaround for that.CSS:
HTML:
您必须绝对定位
nav-bar
和tooltip
(否则 z-index 将不会被考虑在内),并维护map-container
静态定位You have to absolutely position
nav-bar
andtooltip
(otherwise z-index won't be taken in account), and maintainmap-container
static positioned我认为使用
#map-container
上的position:fixed
实现此目的的唯一方法是重构工具提示以显示在#map- 之外
因此,单击地图容器“内部”的图标时,工具提示本身会显示在两者上方(设置了正确的z-index
)。I think the only way you can do this with a
position: fixed
on the#map-container
is to restructure your tool tips to display outside the#map-container.
So on click of the icon "inside" the map container, the tool-tip itself is displayed above both (with a properz-index
set).在查看了你的代码后,我注意到了这一点。
您的 #tooltip 有 z 索引,但未定位。 Z-index 属性仅在具有位置属性值之一时才起作用。考虑到您希望工具提示突出,您应该像这样使用绝对位置值。
HTML
这使 #tooltip 保持在顶部......
After going through, your codes, i noticed this.
Your #tooltip has a z-index, but it's not positioned. Z-index property will only work if it's has one of the position property value. And considering you want the tooltip to stand out, you should use the absolute position value like this.
HTML
This keeps the #tooltip on top....
对于未来遇到类似问题的读者 -
如果冲突的子项是
position:fixed
,请考虑将父容器的高度设置为 0px,然后将任何父级背景显示设置转移到冲突子项的共同祖父上。这解决了我类似的困境。
For future readers with similar problems -
If your conflicting child items are
position: fixed
, consider setting the height of the parent containers to 0px, and then shifting any parent background display settings onto a mutual grandparent of the conflicting children.This solved my analogous delimma.
如果在实际页面中,仅在将鼠标悬停在地图容器上时才需要显示
tooltip
,则您可以像这样动态更改其 z-index:否则您需要更改
的位置>工具提示
,以便导航栏
不会与其重叠。If, in the real page, the
tooltip
has to be shown only on hovering the map container, you could just change dynamically its z-index like so:Otherwise you need to change the position of the
tooltip
so that thenav-bar
doesn't overlap it.