孩子和父母之间的 z-index

发布于 2024-12-21 20:34:59 字数 547 浏览 0 评论 0原文

我在计算我们正在开发的应用程序的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

晌融 2024-12-28 20:34:59

如果 #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. Any z-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.

最初的梦 2024-12-28 20:34:59

下面的解决方案应该有效,但我不知道您是否有将 nav-bar 保留在 map-container 之外的要求。如果是这样,我认为没有解决方法。

CSS:

#tooltip-helper{
    position:relative;
    /*below properties are to demonstrate the helper*/
    width:10px;
    height:10px;
    background-color:green;
    top:200px;
    left:200px;
}

#tooltip
{
    position:absolute;
    top:10px;/*this is just to make sure helper is visible*/
    left:-100px;/*this is to center the tooltip*/
    width: 200px;
    height: 200px;
    background-color: yellow;
    color: black;
    padding: 10px;
    z-index: 15;
}

HTML:

<div id="map-container">
    <div id="nav-bar">
      The nav bar
    </div>
    This is the map container
    <div id="tooltip-helper">
          <div id="tooltip">This is the Tooltip</div>
    </div>
</div>

Below solution should work but I don't know if you have a requirement like keeping nav-bar outside map-container. If so I don't think that there is a workaround for that.

CSS:

#tooltip-helper{
    position:relative;
    /*below properties are to demonstrate the helper*/
    width:10px;
    height:10px;
    background-color:green;
    top:200px;
    left:200px;
}

#tooltip
{
    position:absolute;
    top:10px;/*this is just to make sure helper is visible*/
    left:-100px;/*this is to center the tooltip*/
    width: 200px;
    height: 200px;
    background-color: yellow;
    color: black;
    padding: 10px;
    z-index: 15;
}

HTML:

<div id="map-container">
    <div id="nav-bar">
      The nav bar
    </div>
    This is the map container
    <div id="tooltip-helper">
          <div id="tooltip">This is the Tooltip</div>
    </div>
</div>
二智少女猫性小仙女 2024-12-28 20:34:59

您必须绝对定位 nav-bartooltip (否则 z-index 将不会被考虑在内),并维护 map-container静态定位

#map-container{
    ...
    position: static;
    ...
}

#nav-bar{
    ...
    position: absolute;
}

#tooltip{
    ...
    position: absolute
}

You have to absolutely position nav-bar and tooltip (otherwise z-index won't be taken in account), and maintain map-container static positioned

#map-container{
    ...
    position: static;
    ...
}

#nav-bar{
    ...
    position: absolute;
}

#tooltip{
    ...
    position: absolute
}
很糊涂小朋友 2024-12-28 20:34:59

我认为使用 #map-container 上的 position:fixed 实现此目的的唯一方法是重构工具提示以显示在 #map- 之外 因此,单击地图容器“内部”的图标时,工具提示本身会显示在两者上方(设置了正确的 z-index)。

 <div id="nav-bar">
    The nav bar
  </div>

  <div id="map-container">
      This is the map container
  </div>

  <div id="tooltip">
      This is the Tooltip
  </div>

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 proper z-index set).

 <div id="nav-bar">
    The nav bar
  </div>

  <div id="map-container">
      This is the map container
  </div>

  <div id="tooltip">
      This is the Tooltip
  </div>
白况 2024-12-28 20:34:59

在查看了你的代码后,我注意到了这一点。

 #tooltip{
background-color: yellow;
width: 200px;
height: 200px;
color: black;
padding: 10px;
z-index: 15;
    }

您的 #tooltip 有 z 索引,但未定位。 Z-index 属性仅在具有位置属性值之一时才起作用。考虑到您希望工具提示突出,您应该像这样使用绝对位置值。

 #tooltip{
position: absolute;
background-color: yellow;
width: 200px;
height: 200px;
color: black;
padding: 10px;
z-index: 15;
 }

HTML

<div id="map-container">
<div id="nav-bar">
The nav bar
    </div>
  This is the map container
  <div id="tooltip">
        This is the Tooltip
  </div>
        </div>

这使 #tooltip 保持在顶部......

After going through, your codes, i noticed this.

 #tooltip{
background-color: yellow;
width: 200px;
height: 200px;
color: black;
padding: 10px;
z-index: 15;
    }

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.

 #tooltip{
position: absolute;
background-color: yellow;
width: 200px;
height: 200px;
color: black;
padding: 10px;
z-index: 15;
 }

HTML

<div id="map-container">
<div id="nav-bar">
The nav bar
    </div>
  This is the map container
  <div id="tooltip">
        This is the Tooltip
  </div>
        </div>

This keeps the #tooltip on top....

我不咬妳我踢妳 2024-12-28 20:34:59

对于未来遇到类似问题的读者 -
如果冲突的子项是 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.

半透明的墙 2024-12-28 20:34:59

如果在实际页面中,仅在将鼠标悬停在地图容器上时才需要显示 tooltip,则您可以像这样动态更改其 z-index:

#map-container:hover
{
    z-index: 16
}

否则您需要更改 的位置>工具提示,以便导航栏不会与其重叠。

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:

#map-container:hover
{
    z-index: 16
}

Otherwise you need to change the position of the tooltip so that the nav-bar doesn't overlap it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文