Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap close responsive menu "on click"

On "PRODUCTS" click I slide up a white div (as seen in attached). When in responsive (mobile and tablet), I would like to automaticly close the responsive navbar and only show the white bar.

I tried:

$('.btn-navbar').click();   

also tried:

$('.nav-collapse').toggle(); 

And it does work. However in desktop size, it is also called and does something funky to the menu where it shrinks for a second.

Any ideas?

enter image description here

like image 707
user1040259 Avatar asked Jan 07 '13 20:01

user1040259


People also ask

How do I close the menu when I click outside?

Answer: Use the jQuery on() method You can use the jQuery click() method in combination with the on() method to hide the dropdown menu when the user click outside of the trigger element.

How do you close an open collapse navbar when clicking outside the navbar element in bootstrap 4?

Currently, the only way to open or close it is by clicking on the navbar-toggle button.

How do I make bootstrap navbar collapse?

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".


2 Answers

You don't have to add any extra javascript to what's already included with bootstraps collapse option. Instead simply include data-toggle and data-target selectors on your menu list items just as you do with your navbar-toggle button. So for your Products menu item it would look like this

<li><a href="#Products" data-toggle="collapse" data-target=".navbar-collapse">Products</a></li> 

Then you would need to repeat the data-toggle and data-target selectors for each menu item

EDIT!!! In order to fix overflow issues and flickering on this fix I'm adding some more code that will fix this and still not have any extra javascript. Here is the new code:

<li><a href="#products" class="hidden-xs">Products</a></li> <li><a href="#products" class="visible-xs" data-toggle="collapse" data-target=".navbar-collapse">Products</a></li> 

Here it is at work http://jsfiddle.net/jaketaylor/84mqazgq/

This will make your toggle and target selectors specific to screen size and eliminate glitches on the larger menu. If anyone is still having issues with glitches please let me know and I'll find a fix. Thanks

EDIT: In the bootstrap v4.1.3 & v5.0 I couldnt use visible/hidden classes. Instead of hidden-xs use d-none d-sm-block and instead of visible-xs use d-block d-sm-none.

EDIT: In bootstrap v5, Instead of data-toggle use data-bs-toggle and instead of data-target use data-bs-target.

like image 183
Jake Taylor Avatar answered Oct 13 '22 04:10

Jake Taylor


I've got it to work with animation!

Menu in html:

<div id="nav-main" class="nav-collapse collapse">      <ul class="nav">          <li>              <a href='#somewhere'>Somewhere</a>          </li>      </ul>  </div> 

Binding click event on all a elements in navigation to collapse menu (Bootstrap collapse plugin):

 $(function(){       var navMain = $("#nav-main");      navMain.on("click", "a", null, function () {          navMain.collapse('hide');      });  }); 

EDIT To make it more generic we can use following code snippet

 $(function(){       var navMain = $(".navbar-collapse"); // avoid dependency on #id      // "a:not([data-toggle])" - to avoid issues caused      // when you have dropdown inside navbar      navMain.on("click", "a:not([data-toggle])", null, function () {          navMain.collapse('hide');      });  }); 
like image 33
mollwe Avatar answered Oct 13 '22 04:10

mollwe