Bootstrap 4 可切换导航栏

Posted

技术标签:

【中文标题】Bootstrap 4 可切换导航栏【英文标题】:Bootstrap 4 toggleable navbar 【发布时间】:2018-01-22 09:31:32 【问题描述】:

我正在尝试从 Bootstrap 3.x 迁移到 4,但我遇到了新导航栏的问题。无论我选择 navbar-toggleable-x 的哪个选项,导航栏总是显示汉堡按钮,就像它被折叠一样。

这个 html 文档演示了我的问题。我在官方文档页面上得到了它。

<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
</head>
<body>
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">Navbar</a>
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav">
      <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="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
  </div>
</nav>
</body>
</html>

【问题讨论】:

将您的 &lt;nav&gt; 弹出到 Bootply 并按预期运行:bootply.com/TWBmIowsXy ... 表明该问题与您的语法/bootstrap4 无关。 【参考方案1】:

自 Bootstrap 4 beta 起,toggleable 已替换为 expand

现在它是“扩展”断点,而不是“切换”断点,因此逻辑会相应更改,因此 navbar-toggleable-md 现在将是 navbar-expand-lg..

https://www.codeply.com/go/Io0duaPkqz

<nav class="navbar navbar-expand-lg navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">Navbar</a>
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav">
      <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="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

https://www.codeply.com/go/Io0duaPkqz

如果您希望导航栏水平展开,则需要 navbar-expand* 类,否则导航栏默认为移动的垂直折叠版本。

更多关于 Bootstrap 4 changes from alpha 6 to beta

【讨论】:

以上是关于Bootstrap 4 可切换导航栏的主要内容,如果未能解决你的问题,请参考以下文章

Bootstrap 4 导航栏切换损坏

导航栏切换器不适用于 Bootstrap 4

Bootstrap 4 导航栏切换器未显示

无法在 Angular 的 Bootstrap 4 中切换导航栏

Bootstrap 4 - 导航栏切换不起作用。它没有显示任何导航链接

Bootstrap 4打开多个导航栏下拉菜单,无需切换