z-index无法正常导航

时间:2017-11-16 01:44:02

标签: javascript html css

我正在尝试将汉堡包菜单排在最前面#34;侧导航,以后用这个汉堡包切换sidenav(而不是用x关闭它)。

问题是z-index似乎被忽略了。为什么会这样,即使sidenav进来,我怎么能解决这个问题才能将汉堡带到前面呢?



function openNav() {
    document.getElementById("myNav").style.width = "100%";
}

function closeNav() {
    document.getElementById("myNav").style.width = "0%";
}

.overlay {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: blue;
    overflow-x: hidden;
    transition: 0.5s;
}

.overlay-content {
    position: relative;
    top: 25%;
    width: 100%;
    text-align: center;
    margin-top: 30px;
}

.overlay a {
    padding: 8px;
    text-decoration: none;
    font-size: 36px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.overlay a:hover, .overlay a:focus {
    color: #f1f1f1;
}

.overlay .closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
}

.front {
    z-index: 1050;
}

<div id="myNav" class="overlay">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <div class="overlay-content">
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
  </div>
</div>

<h2>Fullscreen Overlay Nav Example</h2>
<span class="front" style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

请检查.front的{​​{1}}媒体资源。 positionposition: static;忽略position: initial;

如果您想在不更改位置的情况下提供z-index,请更改为z-index

position: relative;
function openNav() {
    document.getElementById("myNav").style.width = "100%";
}

function closeNav() {
    document.getElementById("myNav").style.width = "0%";
}
.overlay {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: blue;
    overflow-x: hidden;
    transition: 0.5s;
}

.overlay-content {
    position: relative;
    top: 25%;
    width: 100%;
    text-align: center;
    margin-top: 30px;
}

.overlay a {
    padding: 8px;
    text-decoration: none;
    font-size: 36px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.overlay a:hover, .overlay a:focus {
    color: #f1f1f1;
}

.overlay .closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
}

.front {
    position: absolute;
    z-index: 1050;
}

答案 1 :(得分:1)

Z-index仅适用于定位元素,也仅适用于自己的堆叠上下文。 <span class="front" ...">&#9776; open</span>未定位。您可以阅读Project's property文章了解详情。可能重复:this

答案 2 :(得分:1)

front课程添加到您的h2元素,并将position: relative添加到front样式:

&#13;
&#13;
function openNav() {
    document.getElementById("myNav").style.width = "100%";
}

function closeNav() {
    document.getElementById("myNav").style.width = "0%";
}
&#13;
.overlay {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: blue;
    overflow-x: hidden;
    transition: 0.5s;
}

.overlay-content {
    position: relative;
    top: 25%;
    width: 100%;
    text-align: center;
    margin-top: 30px;
}

.overlay a {
    padding: 8px;
    text-decoration: none;
    font-size: 36px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.overlay a:hover, .overlay a:focus {
    color: #f1f1f1;
}

.overlay .closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
}

.front {
    z-index: 1050;
    position: relative;
}
&#13;
<div id="myNav" class="overlay">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <div class="overlay-content">
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
  </div>
</div>

<h2 class="front">Fullscreen Overlay Nav Example</h2>
<span class="front" style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>
&#13;
&#13;
&#13;