修复了 header&sidebar z-index 问题

Fixed header&sidebar z-index issue

好久不见! 我目前正在尝试解决一个问题,我希望有人能帮助我。 我有一个固定的侧边栏和一个 header。两者都投射阴影。但是,我不希望 header 在侧边栏上投射阴影(我希望它们处于同一水平)。同时,header 包含 drop-downs 并且这些需要悬停在所有内容上。 由于它相当复杂,我创建了一个 jsfiddle。

Simple example

出于某种原因,我也被迫将代码粘贴到此处(SO 输入验证)。

<div class="layout">
  <div class="header z-depth-2">
    <div class="dropdown-toogle">
      <div class="dropdown-menu">
      </div>
    </div>
  </div>
  <div class="page-wrapper">
    <div class="sidebar z-depth-2">
    </div>
    <div class="content">
    </div>
  </div>
</div>

和css

.layout {
  width: 100%;
  height: 100%;
  position: relative;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 70px;
  background-color: blue;
  z-index: 101;
}

.sidebar {
  position: fixed;
  top: 70px;
  left: 0;
  width: 200px;
  background-color: green;
  bottom: 0;
  z-index:101;
}

.content {
  padding-left: 200px;
  width: 100%;
  height: 100%;
}

.page-wrapper {
  padding-top: 70px;
  height: 100%;
}

.dropdown-toogle {
  margin-right: 100px;
  float: right;
  position: relative;
  height: 100%;
  width: 50px;
  background-color: yellow;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  width: 400px;
  height: 200px;
  background-color: grey;
  z-index:1000;
}

.z-depth-2 {
  box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
}

请注意,当您更改 view-port 宽度时,即使 z-index 更大,drop-down 也会滑动到边栏后面。

如果我增加 header 的 z-index,它开始在边栏上投射阴影(drop-down 开始工作),这是我想避免的。我一直在玩不同的组合,但无法正确整理出来。

希望我能说清楚,帮助非常感谢!

使用您当前的布局,这是不可能的。由于 header 设置为侧边栏后面的 z-index,因此 header 的任何 child 也将位于侧边栏后面。 (阅读更多关于 stacking contexts

要解决此问题,您可以在内容窗格中使用插图 box-shadow。这将使它有一个阴影,就好像它是由 header 和侧边栏投射的一样,但它实际上是容器投射在它自己身上。这样您就不必担心 header 投射到侧边栏上,侧边栏可以安全地位于 "behind" 和 header 上。有了这个,你根本不需要 fiddle 和 z-index(尽管我没有删除它们以防页面上的其他内容需要它)。

为了让它正常工作,我不得不更改 padding 你用来定位内容元素的 margin 但老实说,这是一个更好的 属性 使用在元素周围添加 space。我建议您也阅读一下何时使用 padding vs margin.

https://jsfiddle.net/79gykeu3/6/

HTML

<div class="layout">
  <div class="header z-depth-2">
    <div class="dropdown-toogle">
      <div class="dropdown-menu">
      </div>
    </div>
  </div>
  <div class="page-wrapper">
    <div class="sidebar z-depth-2">
    </div>
    <div class="content">
    </div>
  </div>
</div>

CSS

需要注意的是 .content class 现在有 box-shadow

html {
  height: 100%;
}

body {
  width: 100%;
  height: 100%;
  margin: 0;
}

.layout {
  width: 100%;
  height: 100%;
  position: relative;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 70px;
  background-color: blue;
  z-index: 103;
}

.sidebar {
  position: fixed;
  top: 70px;
  left: 0;
  width: 200px;
  background-color: green;
  bottom: 0;
  z-index:101;
}

.content {
  margin-left: 200px;
  width: 100%;
  height: 100%;
    box-shadow: inset 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
}

.page-wrapper {
  margin-top: 70px;
  height: 100%;
}

.dropdown-toogle {
  margin-right: 100px;
  float: right;
  position: relative;
  height: 100%;
  width: 50px;
  background-color: yellow;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  width: 70%;
  height: 200px;
  background-color: grey;
  z-index:1000;
}

.z-depth-2 {
}

首先,非常感谢 Don,他的方法完全有效、聪明,甚至可能比我使用的方法更好。 但是我想我会 post 我的最终解决方案以及某些人可能认为有 2 种方法。我所做的只是在 header 下方和侧边栏右侧添加了一个 "fake" 渐变。 我这样做的原因基本上是我使用了我知道并且可以依赖的技术。我也不喜欢内容上的溢出自动。无论如何,就是这样:

https://jsfiddle.net/79gykeu3/11/

<div class="layout">
    <div class="header">
        <div class="header__shadow">
        </div>
        <div class="dropdown-toogle">
            <div class="dropdown-menu">
            </div>
        </div>
    </div>
    <div class="page-wrapper">
        <div class="sidebar">
            <div class="sidebar__shadow">
            </div>
        </div>
        <div class="content">
        </div>
    </div>
</div>

Css

.layout {
    width: 100 %;
    height: 100 %;
    position: relative;
}

.header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 70px;
    background - color: blue;
    z - index: 103;
}

.sidebar {
    position: fixed;
    top: 70px;
    left: 0;
    width: 200px;
    background - color: green;
    bottom: 0;
    z - index:101;
}

.content {
    padding - left: 200px;
    height: 8000px;
}

.page - wrapper {
    padding - top: 70px;
    height: 100 %;
}

.dropdown - toogle {
    margin - right: 100px;
    float: right;
    position: relative;
    height: 100 %;
    width: 50px;
    background - color: yellow;
}

.dropdown - menu {
    position: absolute;
    top: 100 %;
    right: 0;
    width: 400px;
    height: 200px;
    background - color: grey;
    z - index:1000;
}

.header__shadow {
    position: absolute;
    height: 7px;
    left: 200px;
    right: 0;
    top: 100 %;
    background: linear - gradient(to bottom, rgba(0, 0, 0, 0.35) 0%,rgba(0, 0, 0, 0) 100%);
}

.sidebar__shadow {
    position: absolute;
    left: 100 %;
    top: 0;
    bottom: 0;
    width: 7px;
    background: linear - gradient(to right, rgba(0, 0, 0, 0.35) 0%,rgba(0, 0, 0, 0) 100%);
}