单击菜单时,汉堡菜单消失

时间:2015-05-28 09:34:02

标签: javascript jquery html css hamburger-menu

如果屏幕宽度小于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");

});

1 个答案:

答案 0 :(得分:1)

只需将链接添加为slideToggle的另一个触发器,例如,还可以在点击任何nav a时切换。

$("img.trigger, nav a").on("click", function(){

    $("nav").slideToggle("slow");

});