gpt4 book ai didi

javascript - 单击后关闭下拉菜单

转载 作者:可可西里 更新时间:2023-11-01 13:02:25 25 4
gpt4 key购买 nike

我为我的网页创建了一个汉堡包下拉菜单。但是我注意到单击其中一个链接后下拉菜单没有关闭。我确实尝试在网上找到解决方案,但没有一个有效。请帮忙!

.header {
margin-top: 60px; /* only to clear the 'Full page' button in the snippet */
background-color: #000;
box-shadow: 1px 1px 4px 0 rgba(0, 0, 0, .1);
width: 100%;
z-index: 3;
}

.header ul {
margin: 0;
padding: 0;
list-style: none;
overflow: hidden;
}

.header li a {
display: block;
padding: 20px 20px;
/*border-right: 1px solid #f4f4f4;*/
text-decoration: none;
}

#rmb_call {
display: block;
padding: 20px 20px;
/*border-right: 1px solid #f4f4f4;*/
text-decoration: none;
}

#rmb_call:hover {
background-color: gray;
}

.header li a:hover {
background-color: gray;
}

.header .logo {
display: block;
float: left;
font-size: 2em;
padding: 10px 20px;
text-decoration: none;
}

.header .menu {
clear: both;
max-height: 0;
transition: max-height .2s ease-out;
}

.header .menu-icon {
cursor: pointer;
display: inline-block;
float: right;
padding: 28px 20px;
position: relative;
user-select: none;
}

.header .menu-icon .navicon {
background: #fff;
display: block;
height: 2px;
position: relative;
transition: background .2s ease-out;
width: 18px;
}

.header .menu-icon .navicon:before,
.header .menu-icon .navicon:after {
background: #fff;
content: '';
display: block;
height: 100%;
position: absolute;
transition: all .2s ease-out;
width: 100%;
}

.header .menu-icon .navicon:before {
top: 5px;
}

.header .menu-icon .navicon:after {
top: -5px;
}

.header .menu-btn {
display: none;
}

.header .menu-btn:checked ~ .menu {
max-height: 410px;
}

.header .menu-btn:checked ~ .menu-icon .navicon {
background: transparent;
}

.header .menu-btn:checked ~ .menu-icon .navicon:before {
transform: rotate(-45deg);
}

.header .menu-btn:checked ~ .menu-icon .navicon:after {
transform: rotate(45deg);
}

.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:before,
.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:after {
top: 0;
}

@media (min-width: 994px) {
.header li {
float: left;
}
.header li a {
padding: 20px 30px;
}
#rmb_call {
float: left;
}
#rmb_call:hover {
padding: 20px 30px;
}
.header .menu {
clear: none;
float: right;
max-height: none;
}
.header .menu-icon {
display: none;
}
}
<header class="header">
<a href="test2.php" width="100%"><img id="scsLogo2" src="img/logo2.jpg" style="margin-top: 5px; margin-left:5px; height: 50px; width: 75px;" /></a>
<input class="menu-btn" type="checkbox" id="menu-btn" />
<label class="menu-icon" for="menu-btn"><span class="navicon"></span></label>
<ul class="menu">
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#membership" class="page-scroll">Membership</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#cert">Certificates</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#highlights">Highlights</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#events">Events</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><span id="rmb_call" onclick="javascript:window.location.href='tel:'">Call Us</span></li>
</ul>
</header>

最佳答案

由于您使用 JavaScript 和 jQuery 标记了问题....

您可以通过在单击菜单项时触发关闭按钮上的单击事件来实现,如下所示:

$('.menu').on('click', 'li', function(){
$('#menu-btn').trigger('click');
});

$('.menu').on('click', 'li', function(){
$('#menu-btn').trigger('click');
});
.header {
margin-top: 60px; /* only to clear the 'Full page' button in the snippet */
background-color: #000;
box-shadow: 1px 1px 4px 0 rgba(0, 0, 0, .1);
width: 100%;
z-index: 3;
}

.header ul {
margin: 0;
padding: 0;
list-style: none;
overflow: hidden;
}

.header li a {
display: block;
padding: 20px 20px;
/*border-right: 1px solid #f4f4f4;*/
text-decoration: none;
}

#rmb_call {
display: block;
padding: 20px 20px;
/*border-right: 1px solid #f4f4f4;*/
text-decoration: none;
}

#rmb_call:hover {
background-color: gray;
}

.header li a:hover {
background-color: gray;
}

.header .logo {
display: block;
float: left;
font-size: 2em;
padding: 10px 20px;
text-decoration: none;
}

.header .menu {
clear: both;
max-height: 0;
transition: max-height .2s ease-out;
}

.header .menu-icon {
cursor: pointer;
display: inline-block;
float: right;
padding: 28px 20px;
position: relative;
user-select: none;
}

.header .menu-icon .navicon {
background: #fff;
display: block;
height: 2px;
position: relative;
transition: background .2s ease-out;
width: 18px;
}

.header .menu-icon .navicon:before,
.header .menu-icon .navicon:after {
background: #fff;
content: '';
display: block;
height: 100%;
position: absolute;
transition: all .2s ease-out;
width: 100%;
}

.header .menu-icon .navicon:before {
top: 5px;
}

.header .menu-icon .navicon:after {
top: -5px;
}

.header .menu-btn {
display: none;
}

.header .menu-btn:checked ~ .menu {
max-height: 410px;
}

.header .menu-btn:checked ~ .menu-icon .navicon {
background: transparent;
}

.header .menu-btn:checked ~ .menu-icon .navicon:before {
transform: rotate(-45deg);
}

.header .menu-btn:checked ~ .menu-icon .navicon:after {
transform: rotate(45deg);
}

.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:before,
.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:after {
top: 0;
}

@media (min-width: 994px) {
.header li {
float: left;
}
.header li a {
padding: 20px 30px;
}
#rmb_call {
float: left;
}
#rmb_call:hover {
padding: 20px 30px;
}
.header .menu {
clear: none;
float: right;
max-height: none;
}
.header .menu-icon {
display: none;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header class="header">
<a href="test2.php" width="100%"><img id="scsLogo2" src="img/logo2.jpg" style="margin-top: 5px; margin-left:5px; height: 50px; width: 75px;" /></a>
<input class="menu-btn" type="checkbox" id="menu-btn" />
<label class="menu-icon" for="menu-btn"><span class="navicon"></span></label>
<ul class="menu">
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#membership" class="page-scroll">Membership</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#cert">Certificates</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#highlights">Highlights</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#events">Events</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><span id="rmb_call" onclick="javascript:window.location.href='tel:'">Call Us</span></li>
</ul>
</header>

关于javascript - 单击后关闭下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37394044/

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