Bootstrap 4:在导航栏折叠时显示活动导航项

Posted

技术标签:

【中文标题】Bootstrap 4:在导航栏折叠时显示活动导航项【英文标题】:Bootstrap 4: Show active nav-item on Navbar collapse 【发布时间】:2018-09-03 00:05:18 【问题描述】:

我正在尝试使用在特定视口宽度处折叠的引导程序创建一个导航栏。现在,我想看到的是,如果导航栏折叠,navbar-brand 会消失,并且当前活动的导航项(例如“主页”、“关于”等)将在左侧可见,从而使访问者能够看看他们在哪一页。

我是一个完全的引导菜鸟,我有点希望在导航栏折叠/展开时看到要添加的 CSS 类,但它不会那样工作。

<html lang="en"><head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  </head>
 <body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <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="#">Project name</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

</body></html>

【问题讨论】:

你必须使用 Bootstrap 3 吗?使用 Bootstrap 4 会更容易。而且,您想要在导航栏的折叠/切换部分中唯一的就是品牌? 不行也可以使用 bootstrap 4 :) 这个想法是当brand + nav items变得太宽时,它会崩溃。然后品牌消失,导航项目可以用汉堡菜单打开。但在左侧(品牌曾经所在的位置)的折叠状态下,它应该显示“Home”(或:“About”、“Contact”等)。我有点希望这可以在没有太多 jquery 魔法的情况下开箱即用:/ 【参考方案1】:

不完全是“开箱即用”,但一点 CSS 将适用于 Bootstrap 4(没有 jquery 或 dup 标记)...

https://www.codeply.com/go/5T4icATKjl

这会覆盖导航栏折叠行为以使用visibility 而不是display。这样我们就可以控制active nav-item 的显示。

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" ></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    
<style>
.navbar .collapse 
    visibility: hidden;
    display: block;


.navbar .collapse.show 
    visibility: visible;


@media (max-width: 767px) 
    .navbar-nav .nav-link.active 
        visibility: visible;
    
    
    .collapsing 
        transition: all 0.35s ease;
        height: 40px !important;
    
    
    .collapse:not(.show) .navbar-nav .nav-item:not(.active) 
        display: none;
    


@media (min-width: 768px) 
    .navbar .collapse .navbar-nav .nav-item 
        visibility: visible;
    

</style>
<body>
<nav class="navbar navbar-expand-md fixed-top navbar-dark bg-dark">
    <div class="container">
        <div class="collapse" id="navbar1">
            <div class="navbar-nav flex-md-row">
                <a class="nav-item navbar-brand" href="#">Project name</a>
                <a class="nav-item nav-link" href="#">Home</a>
                <a class="nav-item nav-link" href="#">About</a>
                <a class="nav-item nav-link active" href="#">Contact</a>
            </div>
        </div>
        <button class="navbar-toggler align-self-start" type="button" data-toggle="collapse" data-target="#navbar1">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>
</nav>
</body>

注意@media 查询与 768px 的 navbar-expand-md 断点一起出现。如果需要,您可以更改它以使用不同的断点。

navbar-expand-sm - 576pxnavbar-expand-lg - 992pxnavbar-expand-xl - 1200px


还有另一种使用重复导航栏导航链接的解决方案,但这需要额外的标记,我认为上面唯一的 CSS 解决方案更简洁。

【讨论】:

是的,这就是我所追求的!谢谢!【参考方案2】:

无法直接在引导程序中使用,但您可以在此处编写一个小的 javascript 代码:

function displayActiveMenu() 
  if ($(window).width() >= 768) 
    // do nothing if we are not collapsed
    return;
  

  var element = $('li.active', '#navbar').clone();
  $('#active-navbar').html(element);


displayActiveMenu(); // for the first time
$(window).on('resize', displayActiveMenu);
@media (max-width: 767px) 
  #active-navbar 
    float: right;
    margin: 0;
  
  #active-navbar>li>a 
     padding-top: 15px;
     padding-bottom: 15px;
  
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>

<body>
  <nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>

          <!-- this is the new element -->
          <ul class="nav navbar-nav visible-xs" id="active-navbar">
          </ul>

          <a class="navbar-brand" href="#">Project name</a>
      </div>

      <div id="navbar" class="collapse navbar-collapse">
        <ul class="nav navbar-nav">
          <li class="active"><a href="#">Home</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#contact">Contact</a></li>
        </ul>
      </div>
      <!--/.nav-collapse -->
    </div>
  </nav>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script>
    window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')
  </script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

</body>

</html>

【讨论】:

这不是我所追求的。你正在复制引导程序的折叠/展开行为。我真正想要的只是在导航栏折叠时显示active 项目。 更新了答案【参考方案3】:

试试这个解决方案。虽然这需要进一步提高导航菜单链接的可点击性。

<html lang="en">
  <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  </head>
  <body>
        <nav class="navbar navbar-inverse navbar-fixed-top">
          <div class="container">
            <div class="navbar-header">
              <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                <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="#">Project name</a>
            </div>
            <div id="navbar" class="collapse navbar-collapse">
              <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#about">About</a></li>
                <li><a href="#contact">Contact</a></li>
              </ul>
            </div><!--/.nav-collapse -->
          </div>
        </nav>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
      <script>
          $ = window.jQuery
          $(document).ready(function()
            $('.navbar-brand').attr('data-brand',$('.navbar-brand').text())
               var txtMenu = $('#navbar ul.navbar-nav .active').text();
               $('.navbar-brand').text(txtMenu)
               $('nav.navbar').on('hide.bs.collapse', function() 
               var txtM = $('#navbar ul.navbar-nav .active').text();
               $('.navbar-brand').text(txtM)
            )
            $('nav.navbar').on('show.bs.collapse', function() 
               var txtB = $('.navbar-brand').attr('data-brand');
               $('.navbar-brand').text(txtB)
            )
          )
      </script>
</body></html>

【讨论】:

谢谢,@seethrough。实际上我有点希望会有一个更漂亮的解决方案..这对我来说似乎真的很hacky(虽然我到目前为止还不是引导专家,所以也许这在引导中是正常的)。

以上是关于Bootstrap 4:在导航栏折叠时显示活动导航项的主要内容,如果未能解决你的问题,请参考以下文章

Bootstrap:导航栏:导航侧菜单:在小屏幕上折叠

JavaScript - Bootstrap 4 - 仅在折叠时隐藏导航栏

Bootstrap 4导航栏不会在大屏幕上折叠

Bootstrap 4导航栏折叠菜单右对齐

Bootstrap 4 - 折叠外的导航栏项目

Bootstrap 4 导航栏不会在 Angular 4 中折叠