我创建了一个导航栏,其中包含“切换”按钮,我遇到的问题是列表项之间的空格,并且此https://stackoverflow.com/a/5256640/9947640方法已解决了该问题。不幸的是,它造成了另一个大问题,问题是我的切换按钮不起作用。
我不明白自己在做什么错,应该怎么办?
$(document).ready(function(){
$(".menu-icon").on("click",function(){
$("nav ul").toggleClass("showing");
});
});
//Scrolling Effect
$(window).on("scroll", function(){
if ($(window).scrollTop()) {
$('nav').addClass('black');
}else{
$('nav').removeClass('black');
}
});
html, body {
margin: 0;
padding: 0;
width: 100%;
}
.content {
width: 90%;
margin: 4em auto;
font-size: 20px;
line-height: 30px;
text-align: justify;
}
.logo {
position: absolute;
float: left;
margin: 16px 46px;
color: #333;
font-weight: bold;
font-size: 20px;
letter-spacing: 2px;
}
nav{
position: fixed;
width: 100%;
}
nav ul{
list-style-type: none;
background: rgba(0,0,0,0);
overflow: hidden;
color: #fff;
padding: 0;
text-align: center;
margin:0;
transition: 1.1s;
font-size:0;
}
nav.black ul{
background-color: #000000;
}
nav ul li{
display: inline-block;
font-size:14px;
padding: 20px;
background-color:#ffab00;
}
nav ul li a{
text-decoration: none;
color: #333333;
font-size: 20px;
}
.menu-icon{
width: 100%;
background-color: #000000;
text-align: right;
box-sizing: border-box;
padding: 15px 2px;
cursor: pointer;
color: #fff;
display: none;
}
nav #right-menu{
position: absolute;
right: 0;
}
nav #right-menu ul{
text-align: right;
}
@media(max-width: 768px){
.logo{
position: fixed;
top: 0;
margin-top: 16px;
}
nav ul{
max-height: 0px;
background-color: #000000;
}
nav.black ul{
background: #000000;
}
.showing {
max-height: 20em;
}
nav ul li{
box-sizing: border-box;
width: 100%;
padding: 15px 0;
text-align: center;
}
.menu-icon{
display: block;
padding-right: 30px;
}
nav #right-menu{
position: relative;
}
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<header>
<nav>
<div class="menu-icon">
<i class="fas fa-bars fa-2x"></i>
</div>
<div class="logo">
Logo
</div>
<div class="menu" id="right-menu">
<ul >
<li><a href="#">HOME</a></li>
<li><a href="#">ABOUT</a></li>
</ul>
</div>
<div class="menu">
<ul>
<li><a href="#">HOME</a></li>
<li><a href="#">ABOUT</a></li>
<li><a href="#">SERVICE</a></li>
</ul>
</div>
</nav><!----- nav ------>
</header><!----- header ------>
答案 0 :(得分:1)
nav ul.showing
在CSS中丢失了吗?
我在nav ul.showing { display: block; }
中添加了display: none;
和nav ul
以使其起作用...
编辑:同时在导航ul中删除overflow: hidden
$(document).ready(function(){
$(".menu-icon").on("click",function(){
$("nav ul").toggleClass("showing");
});
});
//Scrolling Effect
$(window).on("scroll", function(){
if ($(window).scrollTop()) {
$('nav').addClass('black');
}else{
$('nav').removeClass('black');
}
});
html, body {
margin: 0;
padding: 0;
width: 100%;
}
.content {
width: 90%;
margin: 4em auto;
font-size: 20px;
line-height: 30px;
text-align: justify;
}
.logo {
position: absolute;
float: left;
margin: 16px 46px;
color: #333;
font-weight: bold;
font-size: 20px;
letter-spacing: 2px;
}
nav{
position: fixed;
width: 100%;
}
nav ul.showing {
display: block;
}
nav ul{
list-style-type: none;
background: rgba(0,0,0,0);
color: #fff;
padding: 0;
text-align: center;
margin:0;
transition: 1.1s;
font-size:0;
}
nav.black ul{
background-color: #000000;
}
nav ul li{
display: inline-block;
font-size:14px;
padding: 20px;
background-color:#ffab00;
}
nav ul li a{
text-decoration: none;
color: #333333;
font-size: 20px;
}
.menu-icon{
width: 100%;
background-color: #000000;
text-align: right;
box-sizing: border-box;
padding: 15px 2px;
cursor: pointer;
color: #fff;
display: none;
}
nav #right-menu{
position: absolute;
right: 0;
}
nav #right-menu ul{
text-align: right;
}
@media(max-width: 768px){
.logo{
position: fixed;
top: 0;
margin-top: 16px;
}
nav ul{
display: none;
max-height: 0px;
background-color: #000000;
}
nav.black ul{
background: #000000;
}
.showing {
max-height: 20em;
}
nav ul li{
box-sizing: border-box;
width: 100%;
padding: 15px 0;
text-align: center;
}
.menu-icon{
display: block;
padding-right: 30px;
}
nav #right-menu{
position: relative;
}
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<header>
<nav>
<div class="menu-icon">
<i class="fas fa-bars fa-2x"></i>
</div>
<div class="logo">
Logo
</div>
<div class="menu" id="right-menu">
<ul >
<li><a href="#">HOME</a></li>
<li><a href="#">ABOUT</a></li>
</ul>
</div>
<div class="menu">
<ul>
<li><a href="#">HOME</a></li>
<li><a href="#">ABOUT</a></li>
<li><a href="#">SERVICE</a></li>
</ul>
</div>
</nav><!----- nav ------>
</header><!----- header ------>
答案 1 :(得分:1)
哈桑,显示类的最大高度错误。将其设置为px。 例如
@media (max-width: 768px)
.showing {
max-height: 400px;
}
答案 2 :(得分:1)
只需重置.showing
上的最大高度
$(document).ready(function(){
$(".menu-icon").on("click",function(){
$("nav ul").toggleClass("showing");
});
});
//Scrolling Effect
$(window).on("scroll", function(){
if ($(window).scrollTop()) {
$('nav').addClass('black');
}else{
$('nav').removeClass('black');
}
});
html, body {
margin: 0;
padding: 0;
width: 100%;
}
.content {
width: 90%;
margin: 4em auto;
font-size: 20px;
line-height: 30px;
text-align: justify;
}
.logo {
position: absolute;
float: left;
margin: 16px 46px;
color: #333;
font-weight: bold;
font-size: 20px;
letter-spacing: 2px;
}
nav{
position: fixed;
width: 100%;
}
nav ul{
list-style-type: none;
background: rgba(0,0,0,0);
overflow: hidden;
color: #fff;
padding: 0;
text-align: center;
margin:0;
transition: 1.1s;
font-size:0;
}
nav.black ul{
background-color: #000000;
}
nav ul li{
display: inline-block;
font-size:14px;
padding: 20px;
background-color:#ffab00;
}
nav ul li a{
text-decoration: none;
color: #333333;
font-size: 20px;
}
.menu-icon{
width: 100%;
background-color: #000000;
text-align: right;
box-sizing: border-box;
padding: 15px 2px;
cursor: pointer;
color: #fff;
display: none;
}
nav #right-menu{
position: absolute;
right: 0;
}
nav #right-menu ul{
text-align: right;
}
@media(max-width: 768px){
.logo{
position: fixed;
top: 0;
margin-top: 16px;
}
nav ul{
max-height: 0px;
background-color: #000000;
}
nav.black ul{
background: #000000;
}
.showing {
max-height: none; /* Change this */
}
nav ul li{
box-sizing: border-box;
width: 100%;
padding: 15px 0;
text-align: center;
}
.menu-icon{
display: block;
padding-right: 30px;
}
nav #right-menu{
position: relative;
}
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<header>
<nav>
<div class="menu-icon">
<i class="fas fa-bars fa-2x"></i>
</div>
<div class="logo">
Logo
</div>
<div class="menu" id="right-menu">
<ul >
<li><a href="#">HOME</a></li>
<li><a href="#">ABOUT</a></li>
</ul>
</div>
<div class="menu">
<ul>
<li><a href="#">HOME</a></li>
<li><a href="#">ABOUT</a></li>
<li><a href="#">SERVICE</a></li>
</ul>
</div>
</nav><!----- nav ------>
</header><!----- header ------>