我试图让导航栏上的下拉菜单与身体重叠。我试过z-index:1;但下拉菜单仍由身体覆盖,如何让下拉菜单与身体重叠?
此外,当我尝试在移动模式下使用导航栏时菜单未与导航栏对齐,如何使其与导航栏对齐?
谢谢你的帮助!
(function($) {
$(function() {
$('nav ul li > a:not(:only-child)').click(function(e) {
$(this).siblings('.dropdown').toggle();
$('.dropdown').not($(this).siblings()).hide();
e.stopPropagation();
});
$('html').click(function() {
$('.dropdown').hide();
});
$('#toggle').on('click', function() {
this.classList.toggle('active');
});
$('#toggle').click(function() {
$('nav ul').toggle();
});
});
})(jQuery);
*, body{
margin: 0;
padding: 0;
font-family: 'Open Sans', sans-serif;
box-sizing: border-box;
}
/* Navbar */
nav {
float: right;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav ul li {
float: left;
position: relative;
}
nav ul li a {
display: block;
padding: 0 20px;
line-height: 70px;
background: #ffffff;
color: #333;
text-decoration: none;
}
nav ul li a:hover {
background: #ffffff;
color: #008000;
}
nav ul li a:not(:only-child):after {
padding-left: 1px;
content: ' ▾';
}
nav ul li ul li {
min-width: 190px;
}
nav ul li ul li a {
padding: 15px;
line-height: 20px;
}
.dropdown {
position: absolute;
z-index: 9;
box-shadow: 0 3px 12px rgba(0, 0, 0, 0.15);
display: none;
}
.mobile {
position: absolute;
top: 0;
right: 0;
background: #ffffff;
height: 70px;
width: 70px;
}
@media only screen and (max-width: 800px) {
.mobile {
display: block;
}
nav {
width: 100%;
padding: 70px 0 15px;
}
nav ul {
display: none;
}
nav ul li {
float: none;
}
nav ul li a {
padding: 15px;
line-height: 20px;
}
nav ul li ul li a {
padding-left: 30px;
}
.dropdown {
position: static;
}
nav ul {
display: none;
}
#toggle {
position: absolute;
left: 18px;
top: 22px;
cursor: pointer;
padding: 10px 35px 16px 0px;
}
#toggle span,
#toggle span:before,
#toggle span:after {
color: #333;
cursor: pointer;
border-radius: 1px;
height: 5px;
width: 35px;
background: #333;
position: absolute;
display: block;
content: '';
transition: all 300ms ease-in-out;
}
#toggle span:before {
top: -10px;
}
#toggle span:after {
bottom: -10px;
}
#toggle.active span {
background-color: transparent;
}
#toggle.active span:before,
#toggle.active span:after {
top: 0;
}
#toggle.active span:before {
transform: rotate(45deg);
}
#toggle.active span:after {
transform: rotate(-45deg);
}
}
@media screen and (min-width: 800px) {
.list {
display: block !important;
}
}
.navigation {
height: 70px;
background: #ffffff;
}
.container {
max-width: 1500px;
margin: 0 auto;
}
.brand {
font-size: 20px;
position: relative;
padding: 20px;
width: 200px;
float: left;
text-transform: none;
}
.brand a,
.brand a:visited {
color: #333;
text-decoration: none;
}
/* Body */
.body{
box-sizing: border-box;
z-index: -1;
}
.container {
width:90%;
max-width: 1300px;
margin: 2em auto;
background-color:#fff;
overflow:hidden;
padding:.5em;
}
img {
max-width:100%;
}
.item {
width:25%;
float:left;
padding:.5em;
border:1px solid #333;
/*background-color:#fff;*/
}
.item p {
min-height:3em;
/*background-color:#fff;*/
margin:.5em 0 0 0;
font-size:.9em;
text-align:center;
}
@media screen and (max-width:1000px) {
/* body {
background-color:#fff;
} */
.item {
width:33.3%;
}
}
@media screen and (max-width:800px) {
/* body {
background-color:#fff;
} */
.item {
width:50%;
}
.item p {
font-size:.8em;
}
}
@media screen and (max-width:400px) {
/* body {
background-color:#fff;
} */
.item {
width:100%;
}
}
<body>
<section class="navigation">
<div class="container">
<div class="brand">
<a href="#!">Logo</a>
</div>
<nav>
<div class="mobile">
<a id="toggle" href="#!"><span></span></a>
</div>
<ul class="list">
<li><a id="home" href="#!">Item</a></li>
<li><a id="home" href="#!">Item</a>
<ul class="dropdown">
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
</ul>
</li>
<li><a id="home" href="#!">Item</a>
<ul class="dropdown">
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
</ul>
</li>
<li><a id="home" href="#!">Item</a>
<ul class="dropdown">
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
</ul>
</li>
<li><a id="home" href="#!">Item</a></li>
</ul>
</nav>
</div>
</section>
<section class="body">
<div class="container">
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
</div>
</section>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>
你这里不需要z-index
。问题是你将overflow:hidden
添加到.container
。你在.container
里面有一个.navigation
。
.navigation .container {
overflow: visible;
}
会解决它。
您可能想要从z-index: -1
中删除.body
。
(function($) {
$(function() {
$('nav ul li > a:not(:only-child)').click(function(e) {
$(this).siblings('.dropdown').toggle();
$('.dropdown').not($(this).siblings()).hide();
e.stopPropagation();
});
$('html').click(function() {
$('.dropdown').hide();
});
$('#toggle').on('click', function() {
this.classList.toggle('active');
});
$('#toggle').click(function() {
$('nav ul').toggle();
});
});
})(jQuery);
*, body{
margin: 0;
padding: 0;
font-family: 'Open Sans', sans-serif;
box-sizing: border-box;
}
/* Navbar */
nav {
float: right;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav ul li {
float: left;
position: relative;
}
nav ul li a {
display: block;
padding: 0 20px;
line-height: 70px;
background: #ffffff;
color: #333;
text-decoration: none;
}
nav ul li a:hover {
background: #ffffff;
color: #008000;
}
nav ul li a:not(:only-child):after {
padding-left: 1px;
content: ' ▾';
}
nav ul li ul li {
min-width: 190px;
}
nav ul li ul li a {
padding: 15px;
line-height: 20px;
}
.dropdown {
position: absolute;
z-index: 9;
box-shadow: 0 3px 12px rgba(0, 0, 0, 0.15);
display: none;
}
.mobile {
position: absolute;
top: 0;
right: 0;
background: #ffffff;
height: 70px;
width: 70px;
}
@media only screen and (max-width: 800px) {
.mobile {
display: block;
}
nav {
width: 100%;
padding: 70px 0 15px;
}
nav ul {
display: none;
}
nav ul li {
float: none;
}
nav ul li a {
padding: 15px;
line-height: 20px;
}
nav ul li ul li a {
padding-left: 30px;
}
.dropdown {
position: static;
}
nav ul {
display: none;
}
#toggle {
position: absolute;
left: 18px;
top: 22px;
cursor: pointer;
padding: 10px 35px 16px 0px;
}
#toggle span,
#toggle span:before,
#toggle span:after {
color: #333;
cursor: pointer;
border-radius: 1px;
height: 5px;
width: 35px;
background: #333;
position: absolute;
display: block;
content: '';
transition: all 300ms ease-in-out;
}
#toggle span:before {
top: -10px;
}
#toggle span:after {
bottom: -10px;
}
#toggle.active span {
background-color: transparent;
}
#toggle.active span:before,
#toggle.active span:after {
top: 0;
}
#toggle.active span:before {
transform: rotate(45deg);
}
#toggle.active span:after {
transform: rotate(-45deg);
}
}
@media screen and (min-width: 800px) {
.list {
display: block !important;
}
}
.navigation {
height: 70px;
background: #ffffff;
}
.container {
max-width: 1500px;
margin: 0 auto;
}
.brand {
font-size: 20px;
position: relative;
padding: 20px;
width: 200px;
float: left;
text-transform: none;
}
.brand a,
.brand a:visited {
color: #333;
text-decoration: none;
}
/* Body */
.body{
box-sizing: border-box;
}
.container {
width:90%;
max-width: 1300px;
margin: 2em auto;
background-color:#fff;
overflow:hidden;
padding:.5em;
}
img {
max-width:100%;
}
.item {
width:25%;
float:left;
padding:.5em;
border:1px solid #333;
/*background-color:#fff;*/
}
.item p {
min-height:3em;
/*background-color:#fff;*/
margin:.5em 0 0 0;
font-size:.9em;
text-align:center;
}
@media screen and (max-width:1000px) {
/* body {
background-color:#fff;
} */
.item {
width:33.3%;
}
}
nav > li:hover {
z-index: 1;
}
@media screen and (max-width:800px) {
/* body {
background-color:#fff;
} */
.item {
width:50%;
}
.item p {
font-size:.8em;
}
}
@media screen and (max-width:400px) {
/* body {
background-color:#fff;
} */
.item {
width:100%;
}
}
.navigation .container {
overflow: visible;
}
<body>
<section class="navigation">
<div class="container">
<div class="brand">
<a href="#!">Logo</a>
</div>
<nav>
<div class="mobile">
<a id="toggle" href="#!"><span></span></a>
</div>
<ul class="list">
<li><a id="home" href="#!">Item</a></li>
<li><a id="home" href="#!">Item</a>
<ul class="dropdown">
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
</ul>
</li>
<li><a id="home" href="#!">Item</a>
<ul class="dropdown">
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
</ul>
</li>
<li><a id="home" href="#!">Item</a>
<ul class="dropdown">
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
</ul>
</li>
<li><a id="home" href="#!">Item</a></li>
</ul>
</nav>
</div>
</section>
<section class="body">
<div class="container">
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
</div>
</section>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>
这可能有所帮助,但代码非常混乱,你需要尽快重构。
创建导航或任何其他类型的叠加层时的一般想法是:
请注意:为了使你的身体不可滚动,有很多更好的选择然后将溢出设置为隐藏,请搜索堆栈。
(function($) {
$(function() {
$('nav ul li > a:not(:only-child)').click(function(e) {
$(this).siblings('.dropdown').toggle();
$('.dropdown').not($(this).siblings()).hide();
e.stopPropagation();
});
$('html').click(function() {
$('.dropdown').hide();
});
$('#toggle').on('click', function() {
this.classList.toggle('active');
});
$('#toggle').click(function() {
$('nav ul').toggle();
});
});
})(jQuery);
*, body{
margin: 0;
padding: 0;
font-family: 'Open Sans', sans-serif;
box-sizing: border-box;
}
/* Navbar */
nav {
float: right;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav ul li {
float: left;
position: relative;
}
nav ul li a {
display: block;
padding: 0 20px;
line-height: 70px;
background: #ffffff;
color: #333;
text-decoration: none;
}
nav ul li a:hover {
background: #ffffff;
color: #008000;
}
nav ul li a:not(:only-child):after {
padding-left: 1px;
content: ' ▾';
}
nav ul li ul li {
min-width: 190px;
}
nav ul li ul li a {
padding: 15px;
line-height: 20px;
}
.dropdown {
position: absolute;
z-index: 9;
box-shadow: 0 3px 12px rgba(0, 0, 0, 0.15);
display: none;
}
.mobile {
position: absolute;
top: 0;
right: 0;
background: #ffffff;
height: 70px;
width: 70px;
}
@media only screen and (max-width: 800px) {
.navigation {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.mobile {
display: block;
}
nav {
width: 100%;
padding: 0 0 15px;
}
nav ul {
display: none;
}
nav ul li {
float: none;
}
nav ul li a {
padding: 15px;
line-height: 20px;
}
nav ul li ul li a {
padding-left: 30px;
}
.dropdown {
position: static;
}
nav ul {
display: none;
}
#toggle {
position: absolute;
left: 18px;
top: 22px;
cursor: pointer;
padding: 10px 35px 16px 0px;
}
#toggle span,
#toggle span:before,
#toggle span:after {
color: #333;
cursor: pointer;
border-radius: 1px;
height: 5px;
width: 35px;
background: #333;
position: absolute;
display: block;
content: '';
transition: all 300ms ease-in-out;
}
#toggle span:before {
top: -10px;
}
#toggle span:after {
bottom: -10px;
}
#toggle.active span {
background-color: transparent;
}
#toggle.active span:before,
#toggle.active span:after {
top: 0;
}
#toggle.active span:before {
transform: rotate(45deg);
}
#toggle.active span:after {
transform: rotate(-45deg);
}
}
@media screen and (min-width: 800px) {
.list {
display: block !important;
}
}
.navigation {
height: 70px;
background: #ffffff;
}
.container {
max-width: 1500px;
margin: 0 auto;
}
.brand {
font-size: 20px;
position: relative;
padding: 20px;
width: 200px;
float: left;
text-transform: none;
}
.brand a,
.brand a:visited {
color: #333;
text-decoration: none;
}
/* Body */
.body{
box-sizing: border-box;
z-index: -1;
}
.container {
width:90%;
max-width: 1300px;
margin: 2em auto;
background-color:#fff;
overflow:hidden;
padding:.5em;
}
img {
max-width:100%;
}
.item {
width:25%;
float:left;
padding:.5em;
border:1px solid #333;
/*background-color:#fff;*/
}
.item p {
min-height:3em;
/*background-color:#fff;*/
margin:.5em 0 0 0;
font-size:.9em;
text-align:center;
}
@media screen and (max-width:1000px) {
/* body {
background-color:#fff;
} */
.item {
width:33.3%;
}
}
@media screen and (max-width:800px) {
/* body {
background-color:#fff;
} */
.item {
width:50%;
}
.item p {
font-size:.8em;
}
}
@media screen and (max-width:400px) {
/* body {
background-color:#fff;
} */
.item {
width:100%;
}
}
<body>
<section class="navigation">
<div class="container">
<div class="brand">
<a href="#!">Logo</a>
</div>
<nav>
<div class="mobile">
<a id="toggle" href="#!"><span></span></a>
</div>
<ul class="list">
<li><a id="home" href="#!">Item</a></li>
<li><a id="home" href="#!">Item</a>
<ul class="dropdown">
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
</ul>
</li>
<li><a id="home" href="#!">Item</a>
<ul class="dropdown">
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
</ul>
</li>
<li><a id="home" href="#!">Item</a>
<ul class="dropdown">
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
</ul>
</li>
<li><a id="home" href="#!">Item</a></li>
</ul>
</nav>
</div>
</section>
<section class="body">
<div class="container">
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
<div class="item">
<img src="http://placehold.it/400x300">
<p>Item Caption</p>
</div>
</div>
</section>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>