【问题标题】:CSS transition and z-indexCSS 过渡和 z-index
【发布时间】:2015-11-25 04:22:12
【问题描述】:

我有一个带有下拉菜单的固定顶部导航栏。我想在悬停时滑动下拉菜单。我希望菜单在滑入时位于导航栏后面。所以我只是尝试设置两个元素的z-index,不幸的是这对我不起作用。

这里是一个简化的例子 (codepen)

html

<div class="fixed-top">
  <span class="trigger">hover me</span>
  <div class="menu"></div>
</div>

css

.fixed-top {
  background: #3b5999;
  height: 50px;
  width: 100%;
  padding: 0;
  top: 0;
  position: fixed;
  z-index: 2;
}

.trigger {
  z-index: 2;
  font-size: 33px;
  color: white;
  margin-left: 50%;
}

.trigger:hover + .menu {
  margin-top: 0;
}

.menu {
  z-index: 1;
  height: 300px;
  background-color: #1c7754;
  width: 400px;
  margin: auto;
  margin-top: -400px;
  transition: all 0.75s ease-out;
}

如果不清楚我想在这里做什么,一个简单的 mspaint 草图;)

【问题讨论】:

  • @Justcode 我的选择器实际上工作正常:D,你的代码结果和我的一样
  • 是的,但是当我点击它时它现在无法正常工作:P
  • 这可以帮助您更好地理解堆叠上下文:stackoverflow.com/a/32515284/3597276

标签: css css-transitions css-animations


【解决方案1】:

当开始在 CSS 中使用堆叠上下文时,这是一个非常常见的错误。基本上,您只需要记住,子级不能存在于与父级不同的堆叠上下文中。

因此,如果我有一个非静态元素(即带有position: anything-but-static [fixed, relative, absolute] 的元素),并且如果该元素没有非静态父元素,那么它将处于堆叠上下文级别 1,无论它在哪里DOM。现在,如果该元素具有非静态子元素,则该子元素将处于堆栈上下文级别 2。它不能与其父元素位于同一级别(级别 1)z-index 只能影响同一堆叠上下文层级的元素,与不同堆叠上下文层级的元素无关。

解决方案是重构您的 HTML,或者只使用 :before:after 伪元素,因此:

.fixed-top {
    height: 50px;
    width: 100%;
    padding: 0;
    top: 0;
    position: fixed; /* The parent: stacking context level 1 */
}
.fixed-top:before {
    background: #3b5999;
    content:'';
    position: absolute; /* stacking context level 2 */
    top: 0; right: 0; bottom: 0; left: 0;
    z-index: 1;
}
.trigger {
    color: white;
    font-size: 33px;
    margin-left: 50%;
    position: relative; /* also stacking context level 2 */
    z-index: 2;
}
.trigger:hover + .menu {
    margin-top: 0;
}
.menu { /* bottom layer -- no stacking context necessary */
    z-index: 0;
    height: 300px;
    background-color: #1c7754;
    width: 400px;
    margin: auto;
    margin-top: -400px;
    transition: all 0.75s ease-out;
}

注意表示堆叠上下文级别的 cmets。

这里有一个JSFiddle 的例子。

【讨论】:

  • 不错,还是需要改成这个:.trigger:hover + .menu, .menu:hover,这样鼠标离开span时菜单就不会上滑了,+1
  • @Mi-Creativity 我不能说这是规范的一部分,但我会为你的建议表示赞赏。
  • 这是我一直在寻找的答案。我现在看到了这些症状,在弄乱了 z-index 之后,我开始强烈地感觉到孩子永远不能在父母之下。我还没有解决它,但在你的帖子和你的例子之间,我应该能够。
【解决方案2】:

尝试运行代码片段。

.fixed-top {
  background: #3b5999;
  height: 50px;
  width: 100%;
  padding: 0;
  top: 0;
  position: fixed;
  z-index: 2;
}

.trigger {
  font-size: 33px;
  color: white;
  margin-left: 50%;
  width: 100%;
  height: 50px;
  top: 0;
  position: fixed;  
  z-index: 3;  
}

.trigger:hover + .menu,
.menu:hover{
  margin-top: 0;
}

.menu {
  z-index: 1;
  height: 300px;
  background-color: #1c7754;
  width: 400px;
  margin: auto;
  margin-top: -400px;
  transition: all 0.75s ease-out;
}
<div class="fixed-top"></div>
<span class="trigger">hover me</span>
<div class="menu"></div>

【讨论】:

  • 这行得通,谢谢。但是我对它不太满意.. :D related question
  • 好吧,如果有人能够在不损害您的 html 框架的情况下实现它,那会感到惊讶:)
  • @kolunar 嗯,你的意思是像我在回答中所做的那样?
  • @bowheart 啊!你搞定了,伪 :before masking 就可以了:D
【解决方案3】:

我想这就是你想要的。

css代码

    .fixed-top {
  background: #3b5999;
  height: 50px;
  width: 100%;
  padding: 0;
  top: 0;
  position : fixed;
}

.trigger {
  z-index: 500;
  font-size: 33px;
  color: white;
  margin-left: 50%;
  position : absolute;
}

.trigger:hover + .menu {
  margin-top: 0;
}

.menu {
  z-index: -9999;
  position : relative;
  height: 300px;
  background-color: #1c7754;
  width: 400px;
  margin: auto;
  margin-top: -400px;
  transition: all 0.75s ease-out;
}

    <div class="fixed-top">
  <span class="trigger">hover me</span>
  <div class="menu"></div>
</div>

这里是你可以检查的参考

http://www.smashingmagazine.com/2009/09/the-z-index-css-property-a-comprehensive-look/

【讨论】:

    猜你喜欢
    • 2014-09-14
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多