Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically toggle bootstrap 3 navigation bar

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

<nav class="navbar navbar-inverse navbar-fixed-top" id="toggleNav" role="navigation">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Brand name</a>
        </div>
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse navbar-ex1-collapse">                
            <ul class="nav navbar-nav navbar-right">
                <li>
                    <a href="javascript:logout();"><span class="glyphicon glyphicon-log-out"></span>&nbsp;Logout</a>
                </li>
                <li>
                    <a href="#" id="online-offline" toggle="offline"><span class="glyphicon glyphicon-off"></span>&nbsp;Go offline</a>
                </li>
            </ul>                
        </div><!-- /.navbar-collapse -->
    </nav> 

When I click on the second item ('Go offline') I use jquery to invoke a modal window. On mobile I have to first collapse the menu to reach the link, and then when I click on that item I get my modal window correctly.

What I need to achieve is to hide the navbar collapse before showing the modal. Is it possible to programmatically toggle the navbar collapse? How can I do that ?

like image 383
bboydflo Avatar asked Feb 10 '14 10:02

bboydflo


People also ask

How do I toggle navbar in Bootstrap?

To create a collapsible navigation bar, use a button with class="navbar-toggler", data-toggle="collapse" and data-target="#thetarget" . Then wrap the navbar content (links, etc) inside a div element with class="collapse navbar-collapse" , followed by an id that matches the data-target of the button: "thetarget".

How do I move navbar elements to the right?

ml-auto class in Bootstrap can be used to align navbar items to the right. The . ml-auto class automatically aligns elements to the right.

How do I put two navbar-collapse content in the same line?

Wrap both #nabar-mid-collapse and #navbar-rt-collapse in a div with class row-fluid , and apply class col-xs-4 (*or any respective . col class according to the width you need for each item *) to both of them. There is no row-fluid in Bootstrap 3.


1 Answers

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

like image 53
BENARD Patrick Avatar answered Oct 04 '22 00:10

BENARD Patrick