Programmatically toggle bootstrap 3 navigation bar

孤者浪人 提交于 2019-12-01 18:14:29

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

bboydflo

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>

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;
          }
      });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!