我想使用jquery制作响应式导航栏,但好像toggleClass没有正确应用。下面是我的JS和HTML:jquery响应菜单无法正常工作
$(document).ready(function() {
$(".burguer-nav").on("click", function() {
$("header nav ul").toggleClass("open");
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="menu.js"> </script>
<style>
@media only screen and (max-width: 480px){
a{
text-align:center;
}
.burguer-nav{
display: block;
height: 40px;
cursor: pointer;
}
header nav ul{
overflow:hidden;
height:0;
background-color: #505050;
}
header nav ul.open{
height:auto;
}
header nav ul li{
float:none;
width:100%;
margin:0;
}
header nav ul li a{
color: #fff;
padding: 10px;
display:block;
margin:0;
border-bottom: 1px solid #404040;
}
}
</style>
</head>
<link rel="stylesheet" href="stylesheet.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
<body>
<header>
<nav>
<a href="" class="burguer-nav"><i class="fa fa-bars" aria-hidden="true"></i> Primary Menu</a>
<ul>
<li><a href=""></a>Home</li>
<li><a href=""></a>About</li>
<li><a href=""></a>Contact</li>
<li><a href=""></a>Services</li>
</ul>
</nav>
</header>
</body>
</html>
此刻,当我点击链接(ANCOR标签)的菜单快速出现和消失,然后再次所以实际上甚至不看菜单项上。 如何才能使切换正常工作?在代码中做错了什么?
由于它工作正常。 如何向它添加更好的转换,使其不会突然下降? –
乐意帮忙@WosleyAlarico。由于我解决了您的问题,请注意并接受我的回答,以便其他人知道您的问题已修复。 至于动画,有很多方法可以解决这个问题,您可以从零开始构建动画或使用jQuery/Boostrap下拉菜单。一些好的资源开始阅读是http://www.w3schools.com/howto/howto_js_dropdown.asp,http://stackoverflow.com/questions/25876195/bootstrap-3-dropdown-transition。 –
@Decron Bernard。答复投票再次感谢支持。 –