JavaScript Angular with bootstrap: How to toggle up the nav bar in responsive mood? [duplicate] angular with bootstrap 4,angular with bootstrap example,angular with

I think you are all over engineering..

    $('.navbar-collapse ul li a').click(function(){ 
            $('.navbar-toggle:visible').click();
    });

EDIT: To take care of sub menus, make sure their toggle anchor has the dropdown-toggle class on it.

    $(function () { 
            $('.navbar-collapse ul li a:not(.dropdown-toggle)').click(function () { 
                    $('.navbar-toggle:visible').click(); 
            }); 
    });

EDIT 2: Add support for phone touch.

    $(function () {
            $('.navbar-collapse ul li a:not(.dropdown-toggle)').bind('click touchstart', function () {
                    $('.navbar-toggle:visible').click();
            });
    });
Answer:1

I really liked Jake Taylor's idea of doing it without additional JavaScript and taking advantage of Bootstrap's collapse toggle. I found you can fix the "flickering" issue present when the menu isn't in collapsed mode by modifying the data-target selector slightly to include the in class. So it looks like this:

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

I didn't test it with nested dropdowns/menus, so YMMV.

Answer:2

just to be complete, in Bootstrap 4.0.0-beta using .show worked for me...

<li>
  <a target='_blank' href="#Products" data-toggle="collapse" data-target=".navbar-collapse.show">Products</a>
</li>
Answer:3

This works, but does not animate.

$('.btn-navbar').addClass('collapsed');
$('.nav-collapse').removeClass('in').css('height', '0');
Answer:4

I'm assuming you have a line like this defining the nav area, based on Bootstrap examples and all

<div class="nav-collapse collapse" >

Simply add the properties as such, like on the MENU button

<div class="nav-collapse collapse" data-toggle="collapse"  data-target=".nav-collapse">

I've added to <body> as well, worked. Can't say I've profiled it or anything, but seems a treat to me...until you click on a random spot of the UI to open the menu, so not so good that.

DK

Answer:5

In the HTML I added a class of nav-link to the a tag of each navigation link.

$('.nav-link').click(
    function () {
        $('.navbar-collapse').removeClass('in');
    }
);
Answer:6

this solution have a fine work, on desktop and mobile.

<div id="navbar" class="navbar-collapse collapse" data-toggle="collapse"  data-target=".navbar-collapse collapse">
Answer:7

Just to spell out user1040259's solution, add this code to your $(document).ready(function() {});

    $('.nav').click( function() {
        $('.btn-navbar').addClass('collapsed');
        $('.nav-collapse').removeClass('in').css('height', '0');
    });

As they mention, this doesn't animate the move... but it does close the nav bar on item selection

Answer:8

For those using AngularJS and Angular UI Router with this, here is my solution (using mollwe's toggle). Where ".navbar-main-collapse" is my "data-target".

Create directive:

module.directive('navbarMainCollapse', ['$rootScope', function ($rootScope) {
    return {
        restrict: 'C',
        link: function (scope, element) {
            //watch for state/route change (Angular UI Router specific)
            $rootScope.$on('$stateChangeSuccess', function () {
                if (!element.hasClass('collapse')) {
                    element.collapse('hide');
                }
            });
        }
    };
}]);

Use Directive:

<div class="collapse navbar-collapse navbar-main-collapse">
    <your menu>
Answer:9

I'm using the mollwe function, although I added 2 improvements:

a) Avoid the dropdown closing if the link clicked is collapsed (including other links)

b) Hide the dropdown too, if you are clicking the visible web content.

jQuery.fn.exists = function() {
                  return this.length > 0;
              }

    $(function() {
                var navMain = $(".navbar-collapse");
                navMain.on("click", "a", null, function() {
                    if ($(this).attr("href") !== "#") {
                        navMain.collapse('hide');
                    }
                });

                $("#content").bind("click", function() {
                     if ($(".navbar-collapse.navbar-ex1-collapse.in").exists()) {
                        navMain.collapse('hide');
                    }
                });

            });
Answer:10

Not the newest thread but i searched for a solution for the same Problem and found one (a mix of some others).

I gave the NavButton:

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

an id / Identifier like:

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

Not the finest "Idea" - but: Works for me! Now you can check up the visibility of your button (with jquery) like:

 var target = $('#navcop');
   if(target.is(":visible")){
   $('#navcop').click();
   }

(NOTE: This is just a Code snipped ! I used a "onclick" Event on my Nav Links! (Starting a AJAX Reguest.)

The result is: If the Button is "visible" it got "clicked" ... So: No Bug if you use the "Fullscreen view" of Bootstrap (width of over 940px).

Greetings Ralph

PS: It works fine with IE9, IE10 and Firefox 25. Didnt checked up others - But i can't see a Problem :-)

Answer:11

This should do the trick.

Requires bootstrap.js.

Example => http://getbootstrap.com/javascript/#collapse

    $('.nav li a').click(function() {
      $('#nav-main').collapse('hide');
    });

This does the same thing as adding 'data-toggle="collapse"' and 'target='_blank' href="yournavigationID"' attributes to navigation menus tags.

Answer:12

This worked for me. I have done like, when i click on the menu button, im adding or removing the class 'in' because by adding or removing that class the toggle is working by default. 'e.stopPropagation()' is to stop the default animation by bootstrap(i guess) you can also use the 'toggleClass' in place of add or remove class.

$('#ChangeToggle').on('click', function (e) {

        if ($('.navbar-collapse').hasClass('in')) {
            $('.navbar-collapse').removeClass('in');
            e.stopPropagation();
        } else {
            $('.navbar-collapse').addClass('in');
            $('.navbar-collapse').collapse();
        }

    });
Answer:13

You cau use

ul.nav {
    display: none;
}

This will by default close the navbar. Please let me know anybody finds this usefull

Answer:14

If for example your toggle-able icon is visible only for extra small devices, then you could do something like this:

$('[data-toggle="offcanvas"]').click(function () {
    $('#side-menu').toggleClass('hidden-xs');
});

Clicking [data-toggle="offcanvas"] will add bootstrap's .hidden-xs to my #side-menu which will hide the side-menu content, but will become visible again if you increase the window size.

Answer:15

if menu html is

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

on nav toggle 'in' class is added and removed from the toggle. check if responsive menu is opened then perform the close toggle.

$('.nav-collapse .nav a').on('click', function(){
    if ( $( '.nav-collapse' ).hasClass('in') ) {
        $('.navbar-toggle').click();
    }
});
Answer:16

Tuggle Nav Close, IE browser compatible answer, without any console error. If you are using bootstrap, use this

$('.nav li a').on('click', function () {
    if ($("#navbar").hasClass("in")) {
        $('.navbar-collapse.in').show();
    }
    else {
        $('.navbar-collapse.in').hide();
    }
})
Answer:17
$('.navbar-toggle').trigger('click');
Answer:18

Following this topic, I am trying to generate a 3D curved triangle as a NURBS surface, but I don't understand how to set up my 3D points to do that. Here is the current implementation : var edges = ...

Following this topic, I am trying to generate a 3D curved triangle as a NURBS surface, but I don't understand how to set up my 3D points to do that. Here is the current implementation : var edges = ...

I must be missing something obvious. Turning to SO after a day of frustrated headbanging ;-) : Given that I have created a time domain axis using d3.time.scale and d3.svg.axis, how can I later modify ...

I must be missing something obvious. Turning to SO after a day of frustrated headbanging ;-) : Given that I have created a time domain axis using d3.time.scale and d3.svg.axis, how can I later modify ...

  1. d3 resize axis
  2. d3 responsive axis

I am trying to translate my application using react-i18next. I know how to use it with simple const components, but not within a class. I'm working with the I18nextProvider. This is my App.js file. ...

I am trying to translate my application using react-i18next. I know how to use it with simple const components, but not within a class. I'm working with the I18nextProvider. This is my App.js file. ...

  1. using react-i18next
  2. react in i18next
  3. use translation react i18next

I'm trying to efficiently make a key-value tour in an array in reactjs. However, I do not know how to display the "key" array without creating another map function. const equipos = [ { key: [ ...

I'm trying to efficiently make a key-value tour in an array in reactjs. However, I do not know how to display the "key" array without creating another map function. const equipos = [ { key: [ ...