Z-index无法处理固定标题和固定下拉菜单

时间:2013-08-18 19:09:45

标签: css3 z-index

我正在尝试设计一个固定的下拉菜单,该菜单会滑动,然后位于我的网站的固定标题下,当它被较小的视图端口查看时。我一定不能正确理解一些东西,因为尽管设置位置正确且使用z-index lower 而不是导航上的父级,它仍然无法正常工作。在下拉列表中,它会在我创建的菜单按钮下滑动,但是在标题上方,然后保持在标题上方。这是我的代码:

HTML:

<header>
    <div id="menu-button" class="up">menu</div>
    <nav role="primary" class="hide">
        <ul>
            <li><a href="#">foobar</a></li>
            <li><a href="#">foobar</a></li>
            <li><a href="#">foobar</a></li>
            <li><a href="#">foobar</a></li>
            <li><a href="#">foobar</a></li>
            <li><a href="#">foobar</a></li>
        </ul>
    </nav>
</header>

CSS:

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
}
header {
    display: block;
    float: left;
    width: 100%;
    height: 50px;
    background-color: hsla(0, 0%, 20%, 1);
    box-shadow: 0px 2px 8px #222;
    position: fixed;
    top: 0;
    z-index: 99;
}
#menu-button {
    display: block;
    float: left;
    background-color: hsla(0, 0%, 50%, 1);
    color: #fff;
    border: 2px solid #222;
    border-radius: 6px;
    padding: .25em .5em;
    margin-left: 10px;
    margin-top: 10px;
    cursor: pointer;
}
.up {
    background-image: linear-gradient(hsla(0, 0%, 100%, .2), hsla(0, 0%, 0%, .2));
}
.down {
    background-image: linear-gradient(hsla(0, 0%, 0%, .2), hsla(0, 0%, 100%, .2));
}
nav ul {
    list-style:none;
    width: 100%;
    position: fixed;
    z-index: -1;
    transition: all .6s ease;
    -webkit-transition: all .6s ease;
    -moz-transition: all .6s ease;
    -o-transition: all .6s ease;
}
.hide ul {
    top: -500px;
}
.reveal ul {
    top: 50px;
}
nav ul li a, nav ul li a:visited {
    display: block;
    float: left;
    width: 50%;
    background-color: hsla(0, 0%, 35%, 1);
    text-decoration: none;
    text-align: center;
    color: #fff;
    padding: 1em;
    border-right: 1px solid #fff;
    border-bottom: 1px solid #fff;
    transition: .4s all;
    -webkit-transition: .4s all;
    -moz-transition: .4s all;
    -o-transition: .4s all;
}
nav ul li:nth-child(even) a {
    border-right: none;
}
nav ul li:hover a {
    background-color: hsla(0, 0%, 50%, 1);
}

jQuery的:

$('#menu-button').click(function () {
    $(this).toggleClass('down');
    $('nav').toggleClass('reveal');
});

这是我为它制作的jsfiddle:

http://jsfiddle.net/kyleshevlin/yaJyK/6/

3 个答案:

答案 0 :(得分:5)

执行此操作并保持固定标头要求的一种方法是在菜单按钮周围添加一个包装器:

<div class='menu-wrap'>
    <div id="menu-button" class="up">menu</div>
</div>
<nav role="primary" class="hide">
    …
</nav>

使菜单包装器成为菜单栏的整个宽度/高度:

.menu-wrap {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: blue; /* just to make it obvious… */
}

Demo fiddle

答案 1 :(得分:1)

删除父(标题)上的z-index并添加位置:relative。

答案 2 :(得分:1)

您不能在固定元素(具有更大的z-index)内的菜单(您想要具有较低的z-index)。 您必须将这两者分开,使两者都固定,并将您的内容放在绝对定位的容器中,顶部=标题高度(或边距顶部)

然后标题和菜单都会保留在原位,您可以单独设置每个z-index(like this

header {
    display: block;
    width: 100%;
    height: 50px;
    background-color: hsla(0, 0%, 20%, 1);
    box-shadow: 0px 2px 8px #222;
    position: fixed;
    top: 0;
    z-index:1;
}
nav{
    position:fixed;
    transition: all .6s ease;
    -webkit-transition: all .6s ease;
    -moz-transition: all .6s ease;
    -o-transition: all .6s ease;
    width:100%;
}

<header>
    <div id="menu-button" class="up">menu</div>
</header>
<nav role="primary" class="hide">
    <ul>
        <li><a href="#">foobar</a></li>
        ...