Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Navbar collapse upon selection?

I've created a Meteor project which uses Twitter Bootstrap for the layout. I have a Nav-bar which is laid out like this: Bootstrap Components

My problem is, on mobile the nav-bar goes into the collapsible mode (which is fine), but when I click the links which route to other templates (using iton:router package). The collapsible menu stays expanded. Is there a way to get it closed upon selecting an item?

like image 575
Barry Michael Doyle Avatar asked Dec 03 '25 15:12

Barry Michael Doyle


1 Answers

Just add:

$(".navbar-toggle").click();

To each route you want (in the router.js file). Or the better option is to define it as a function which you call at each route.

Router.route('/somewhere', function(){
    $(".navbar-toggle").click();
    this.render("navbar", {to:"navbar"});
    this.render("something", {to:"main"});
});

Edit:

Using $('.navbar-toggle').click(); once off instead of applying it to each route, you can do that following in the router.js file:

Router.configure({
  onAfterAction: function() {
    if($('#navbar-collapse').hasClass('in')) {
      $('.navbar-toggle').click();
    }
  }
});
like image 70
StefanL19 Avatar answered Dec 06 '25 09:12

StefanL19



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!