gpt4 book ai didi

css - Z-index 不适用于固定标题和固定下拉菜单

转载 作者:行者123 更新时间:2023-12-05 08:59:03 26 4
gpt4 key购买 nike

我正在尝试设计一个固定的下拉菜单,该菜单可以滑动,然后在我的网站被较小的视口(viewport)查看时位于固定的标题下方。我一定没有正确理解某些东西,因为尽管正确设置了位置并使用了 z-index 低于 nav 上的父项,但它仍然无法正常工作。在下拉时,它会滑到我创建的菜单按钮下方,但会滑到标题上方,然后停留在标题上方。这是我的代码:

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/

最佳答案

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

<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

关于css - Z-index 不适用于固定标题和固定下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18302712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com