简体   繁体   中英

Navigation menu dropdown close on click

I am using bootstrap navigation menu with drop downs.On clicking one menu option its sub menus should open-thats done.Now on clicking the other menu dropdown,the previous one should close-thats done too.Now if the Home drop down is open with its sub menus and if i click on the Home dropdown again,it should close.THats what i want. My code goes as:

<nav class="navbar easy-sidebar" >
    <div class="sample">
        <ul class="list-unstyled main-menu" ng-repeat="menu5 in indController.newArray">
            <li class="dropdown">
                <label class="navbar-collapse-btn" data-toggle="collapse" data-target=".{{menu5.mainMenu}}" id="nav-main">
                    {{menu5.mainMenu}}
                    <b class="menu-caret"></b>
                </label>
                <div class="{{menu5.mainMenu}} collapse" ng-repeat="menu1 in menu5.subMenus" >
                    <a href="#/{{menu1.sub_menu_link}}" class="list-group-item"  onclick="closenav()" ng-click="indController.showheader(menu1.sub_menu_header)">{{menu1.sub_menu_title}} </a>
                </div>
            </li>
        </ul>
    </div>
</nav>

//on clicking one dropdown,the other dd autocloses.
        $('.sample').click(function () {
            var $target = $($(this).data('target'));
            if (!$target.hasClass('in')){
                $('.sample .in').removeClass('in').height(0);
                }
                else{
                    $('.sample .in').addClass('in').height(0);
                }
        });

On click remove all classes 'in' or which one makes your menu opened. something like this:

$('.dropdown').on('click', function(){
    $('.in').removeClass('in');
    // other things..
});

Don't use height in jquery to do this work, better to use classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM