单击后关闭下拉菜单

时间:2016-05-23 14:44:02

标签: javascript jquery html css

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



.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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

因为您使用JavaScript&amp;标记了问题。的jQuery ....

当你点击这样一个菜单项时,可以通过触发关闭按钮上的点击事件来实现:

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

&#13;
&#13;
$('.menu').on('click', 'li', function(){
        $('#menu-btn').trigger('click');
    });
&#13;
.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;
    }
}
&#13;
<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>
&#13;
&#13;
&#13;