How to hide my collapse bootstrap 3 navbar with click on body when, collapse is visible?

前端 未结 7 549
攒了一身酷
攒了一身酷 2021-01-24 23:39

I\'m making a WordPress website for agency where I will go work. I used Bootstrap 3.0, and I created a responsive menu.

How to hide menu when is collapsed and visible (

相关标签:
7条回答
  • 2021-01-24 23:45

    Here is another approach which is working for me.

    My scenario: I want to hide the collapsible when I click anywhere outside of a form which contains a dropdownlist.

      $(document).ready(function(){
          //check if collapsible is shown
          $(".collapse").on('shown.bs.collapse', function(){
            //then check if there was a click
            $(document).click(function(event) {
              //exclude the click was on the form 
              if (!$(event.target).hasClass("input-default")) {
                  //then hide it
                  $(".collapse").collapse('hide');
              }
            });
          });
      });
    
    0 讨论(0)
  • 2021-01-24 23:52

    This works. It will close the collapsed navbar when touched anywhere on the mobile touchscreen.

    $(document).on('touchend', function (event) {
        var clickover = $(event.target);
        var $navbar = $(".navbar-collapse");               
        var _opened = $navbar.hasClass("in");
        if (_opened === true && !clickover.hasClass("navbar-toggle")) {      
            $navbar.collapse('hide');
        }
    });
    
    0 讨论(0)
  • I think your best approach would be to do something in a mediaquery, or pherhaphs with js instead, check in bootstrap page which are the break points and use the one you are interested in.

    0 讨论(0)
  • 2021-01-24 23:58

    You don't want to listen for the click all the time that is so wrong. you have to bind and unbind based on the navbar status. here is my navbar-colapse and it's id is 'z', and when it is open i will listen for clicks and when it's closed i don't listen anymore. i will put comment on the code:

        //CALLBACK AFTER NAVBAR-COLLAPSE OPEN
        $('#z').on('shown.bs.collapse', function () {
            // HERE WE BIND THE CLICK TO THE HANDLER FUNCTION
            $(document).bind( "click", handler );
        })
        //CALLBACK AFTER NAVBAR-COLLAPSE CLOSE
        $('#z').on('hidden.bs.collapse', function () {
            // SINCE THE NAVBAR IS CLOSED WE DONT NEED TO LISTEN FOR CLICKS           ANYMORE
            $(document).unbind( "click", handler );
        })
        //THIS IS THE FUNCTION THAT GET FIRED
        var handler = function() {
            //LISTEN FOR CLICKS
            $(document).click(function(event) {
                //HERE FORM-CONTROL IS MY SEARCH BOX IN THE NAV SO I DONT WANT TO CLOSE
                //THE NAVBAAR IF THEY CLICK ON FONT-CONTROL, BUT ANYTHING BESIDE THAT WE CLOSE THE NAVBAR
                if (!$(event.target).hasClass("form-control")) {
                    $('#z').collapse('hide');
                }
            });
        };  
    

    Also here is my nav-bar code :)

                <nav class="navbar navbar-default my-nav" data-spy="affix" id="nav" style="margin-bottom: 0">
                <div class="container-fluid">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#z" aria-expanded="false">
                            <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="#">-</a>
                    </div>
    
                    <!-- Collect the nav links, forms, and other content for toggling -->
                    <div class="collapse navbar-collapse" id="z">
                        <div class="navbar-form navbar-left my-navbar-left" role="search">
                            <div class="form-group">
                              <input id="searchbar" type="text" class="form-control" placeholder="Search for ads">
                            </div>
                        </div>
                        <ul class="nav navbar-nav navbar-right my-navbar-right">
                            <li><a class="tab-c tab-home"> <span class="text-to-icon">Posts</span> </a></li>
                        </ul>
                    </div><!-- /.navbar-collapse -->
                </div><!-- /.container-fluid -->
            </nav>
    
    0 讨论(0)
  • 2021-01-25 00:04

    Try This Example

    <script type='text/javascript'>
    
        $(document).ready(function () {
            function CloseNav() {
                $(".navbar-collapse").stop().css({ 'height': '1px' }).removeClass('in').addClass("collapse");
                $(".navbar-toggle").stop().removeClass('collapsed');
            }
    
            $('html').click(function (event) {
                var clickover = $(event.target);
                var _opened = $(".navbar-collapse").hasClass("navbar-collapse in");
                if (_opened === true && !clickover.hasClass("navbar-toggle")) {
                    CloseNav();
                }
    
            });
        });
    
    </script>
    

    Update

    you can change the html selector to whatever selector you want, body (if you have enough height), wrapper or whatever. A clean fiddle example here

    0 讨论(0)
  • 2021-01-25 00:04
    $('.collapse').collapse('hide');
    
    0 讨论(0)
提交回复
热议问题