Programmatically toggle bootstrap 3 navigation bar

后端 未结 5 808
一整个雨季
一整个雨季 2021-01-18 13:16

I am using few items in my bootstrap 3 navigation bar grouped in like below:

相关标签:
5条回答
  • 2021-01-18 13:32

    Just use:

    $('.collapse').collapse();
    
    0 讨论(0)
  • 2021-01-18 13:39

    First, add an id to your button tag.

    <button id="idToggleButton" type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
    

    Than add an id to your collapse div

    <div id="divCollapse" class="collapse navbar-collapse navbar-ex1-collapse">
    

    Finally you can programmatically click when divCollapse is visible

    if ($('#divCollapse').is(":visible")){
     $('#idToggleButton').click();
    }
    

    Or when is invisible

    if (!$('#divCollapse').is(":visible")){
     $('#idToggleButton').click();
    }
    
    0 讨论(0)
  • add custom class to toggle navbar (me-toggle) so toggle variable with true value :

       <a href="javascript:;" class="dropdown-toggle me-toggle" data-toggle="dropdown" data-hover="dropdown" data-close-others="true">
            <i class="icon-envelope-open"></i>
            <span class="badge badge-default"> 4 </span>
    
    
    </a>
           <ul class="dropdown-menu ">
                  <li class="external">
                    <h3>You have<span class="bold">7 New</span> Messages</h3>
                    <a href="app_inbox.html">view all</a>
                    </li>
                    <li>
                        item 1
                    </li>
                    <li>
                        item 2
                    </li>
         </ul>
    

    then in click navbar event :

    toggle: true;
        $('a.me-toggle').click(() => {
              toggle = !toggle;
            });
    
            $('.dropdown').on({
              "click": function(event) {            
              },
              "hide.bs.dropdown": function(event) {
                return toggle;
              }
          });
    
    0 讨论(0)
  • 2021-01-18 13:50

    As you said, the event will run a modal.

    So, when your modal ( called yourModal ) is showing (before showing), just hide the menu :

    JS :

    $('.yourModal').on('show.bs.modal', function(){
        $('.navbar-collapse').collapse('hide');
    });
    

    Here are the docs :

    http://getbootstrap.com/javascript/#collapse

    http://getbootstrap.com/javascript/#modals-usage

    0 讨论(0)
  • 2021-01-18 13:51

    After a little bit of help from @Jahnux73 I figured it out myself. So the only thing I had to do is to add :

    data-toggle="collapse" data-target=".navbar-ex1-collapse"
    

    to the specific link that I wanted to toggle the navbar. so the link now looks like following:

    <a href="#" id="online-offline" data-toggle="collapse" 
       data-target=".navbar-ex1-collapse" toggle="offline">
        <span class="glyphicon glyphicon-off"></span>&nbsp;Go offline
    </a>
    
    0 讨论(0)
提交回复
热议问题