如果屏幕宽度小于860像素,则显示JS汉堡菜单。从这个宽度开始,原来的菜单变成了一个堆叠的布局,当点击汉堡菜单图标时,它会滑出。在点击汉堡菜单图标时,切换工作很好,但我希望它在用户点击菜单中的链接时自动消失。有办法做到吗?
相关的HTML:
<header class="scroll-effect">
<!-- Navigation bar -->
<div class="navigation">
<div class="container">
<div class="row>">
<div class="six.columns">
<a href="index.html">
<img src="images/logo-nav.png" alt="BackupChecks Logo">
</a>
</div>
<div class="six.columns">
<nav>
<a href="#customer-portal" class="scroll-button">Customer Portal</a>
<a href="#service-desk" class="scroll-button">Service Desk</a>
<a href="#pricing" class="scroll-button">Pricing</a>
<a href="#contact" class="scroll-button">Contact</a>
</nav>
</div>
</div>
</div>
</div>
<!-- End of navigation bar -->
<div class="header-contents">
<h1>
<img src="images/logo.png" alt="BackupChecks logo" class="animated bounceInDown">
</h1>
<h2 class="animated bounceInDown">
The backup service desk for Asigra
</h2>
<div class="button-box animated fadeInUp">
<a href="#learn-more" class="scroll-button button-green">
Learn more
</a>
<a href="#pricing" class="scroll-button button-white-outline">
Buy it now
</a>
</div>
</div>
</header>
<!-- JS burger menu -->
<img src="images/trigger.png" alt="Menu icon" class="trigger">
<!-- </a> -->
下面是相关的CSS:
.navigation {
background: rgba(255,255,255,1);
height: 70px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
position: fixed;
width: 100%;
top: 0;
z-index: +20;
border-bottom: solid 1px #D9D9D9;
}
header.container {
margin: 0 auto;
max-width: 1000px;
}
.navigation img {
vertical-align: middle;
float: left;
line-height: 70px;
text-align: left;
}
nav {
float: right;
line-height: 70px;
}
nav a {
color: #3E4750;
font-size: 13px;
text-transform: uppercase;
font-weight: 700;
text-decoration: none;
padding: 0 0 0 40px;
display: inline-block;
text-decoration: none;
transition: color ease .4s;
}
nav a:hover {
color: #66CC99;
}
@media screen and (max-width: 860px) {
nav {
margin-top: 70px;
background: #ffffff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
nav a {
display: block;
text-align: center;
padding: 0 30px 0 30px;
border-bottom: solid 1px #D9D9D9;
}
nav {
display: none;
}
img.trigger {
display: block;
position: fixed;
top: 0px;
right: 0px;
}
}
这是我的javascript:
$("img.trigger").on("click", function(){
$("nav").slideToggle("slow");
});
只需添加链接作为slideToggle
的另一个触发器,例如在单击任何nav a
时也可以切换。
$("img.trigger, nav a").on("click", function(){
$("nav").slideToggle("slow");
});