简体   繁体   English

调整大小时引导导航栏隐藏菜单元素

[英]Bootstrap navbar hide menu elements when resizing

I have standard bootstrap menu with many elements and I would like to move those menu elements into stack (where 3 line button are ) button when I change screen size.我有包含许多元素的标准bootstrap菜单,当我更改屏幕大小时,我想将这些菜单元素移动到堆栈(其中 3 个行按钮所在的位置)按钮中。

  <nav class="navbar navbar-expand-md navbar-dark bg-dark mb-4">
  <a class="navbar-brand" href="#">Top navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarCollapse">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
      </li>
    </ul>
    <form class="form-inline mt-2 mt-md-0">
      <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

Does anyone know how to to that?有谁知道怎么做?

The responsive Bootstrap Navbar has only two "states"...响应式 Bootstrap 导航栏只有两个“状态”......

  • Collapsed (vertical)折叠(垂直)
  • Non-collapsed (horizontal)非折叠(水平)

The navbar-expand-* class is used to change the breakpoint between the two states. navbar-expand-*类用于更改两个状态之间的断点。

In your case, you want to maintain the "non-collapsed" horizontal Navbar state, but gradually move nav items to a collapsed menu as needed.在您的情况下,您希望保持“非折叠”水平导航栏状态,但根据需要逐渐将导航项目移动到折叠菜单。 Bootstrap 4 doesn't provide an "out-of-the-box" solution for this. Bootstrap 4 没有为此提供“开箱即用”的解决方案。

However, using the Bootstrap dropdown menu, and some jQuery logic you can progressively move the overflowing navbar items to the dropdown.但是,使用 Bootstrap 下拉菜单和一些 jQuery 逻辑,您可以逐步将溢出的导航栏项目移动到下拉菜单。 Here's the jQuery logic I wrote for this:这是我为此编写的 jQuery 逻辑:

var autocollapse = function (menu,maxHeight) {
    
    var nav = $(menu);
    var navHeight = nav.innerHeight();
    if (navHeight >= maxHeight) {
        $(menu + ' .dropdown').removeClass('d-none');
        $(".navbar-nav").removeClass('w-auto').addClass("w-100");
        while (navHeight > maxHeight) {
            //  add child to dropdown
            var children = nav.children(menu + ' li:not(:last-child)');
            var count = children.length;
            $(children[count - 1]).prependTo(menu + ' .dropdown-menu');
            navHeight = nav.innerHeight();
        }
        $(".navbar-nav").addClass("w-auto").removeClass('w-100');
    }
    else {
        var collapsed = $(menu + ' .dropdown-menu').children(menu + ' li');
      
        if (collapsed.length===0) {
          $(menu + ' .dropdown').addClass('d-none');
        }
      
        while (navHeight < maxHeight && (nav.children(menu + ' li').length > 0) && collapsed.length > 0) {
            //  remove child from dropdown
            collapsed = $(menu + ' .dropdown-menu').children('li');
            $(collapsed[0]).insertBefore(nav.children(menu + ' li:last-child'));
            navHeight = nav.innerHeight();
        }

        if (navHeight > maxHeight) { 
            autocollapse(menu,maxHeight);
        }
    }
}

Demo: https://codeply.com/go/IETSah3bFG演示: https : //codeply.com/go/IETSah3bFG

This works by checking the height of the Navbar as the screen is resized (or page loaded).这是通过在调整屏幕大小(或加载页面)时检查导航栏的高度来实现的。 When the height has increased, the nav items have wrapped and then items are moved to the dropdown.当高度增加时,导航项目已包装,然后项目移动到下拉列表。 When there are no wrapping nav items, the items are removed from the dropdown.当没有包装导航项目时,这些项目将从下拉列表中删除。


Also see: "Mashable" style navbar另请参阅: “Mashable”样式导航栏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM