Bootstrap下拉菜单 - 显示第二个bootstrap导航栏

时间:2016-09-07 14:07:46

标签: html css twitter-bootstrap twitter-bootstrap-3 navbar

我有一个带有开箱即用Exception功能的Bootstrap 3 navbar

有没有办法在按钮的dropdown-menu上显示/转换第二个导航栏?我们想要格式很好的

dropdown-toggle

显示我们的子菜单项。与单击“更多”按钮时www.bbc.co.uk网站的工作方式类似 - 显示第二个集中导航栏而不是<nav class="navbar navbar-default"> <div class="container"> (并在再次按下按钮时隐藏)

2 个答案:

答案 0 :(得分:1)

Bootstrap为您提供了可根据需要使用的不同内置组件。

它还有一个Collapse组件,当我们点击某个特定元素时,它可用于打开和关闭一个块。

您可以在此组件的.navbar.collapse内部使用此组件,如果需要,可以使用更多.dropdown项。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" 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="#">Brand</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <div class="clearfix">
        <ul class="nav navbar-nav">
          <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
          <li><a href="#">Link</a></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="#">Action</a></li>
              <li><a href="#">Another action</a></li>
              <li><a href="#">Something else here</a></li>
              <li role="separator" class="divider"></li>
              <li><a href="#">Separated link</a></li>
              <li role="separator" class="divider"></li>
              <li><a href="#">One more separated link</a></li>
            </ul>
          </li>
        </ul>
        <form class="navbar-form navbar-left">
          <div class="form-group">
            <input type="text" class="form-control" placeholder="Search">
          </div>
          <button type="submit" class="btn btn-default">Submit</button>
        </form>
        <ul class="nav navbar-nav navbar-right">
          <li><a href="#">Link</a></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">Dropdown <span class="caret"></span></a>
          </li>
        </ul>
      </div>
      <div class="collapsible-menu">
        <div class="collapse" id="collapseExample">
          <div class="well">
            Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
          </div>
        </div>
      </div>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

答案 1 :(得分:0)

如果您不只是在Bootstrap 3中寻找多个下拉菜单,就不会天真的支持。但是,您可以实现它。

以下是一些HTML标记示例:

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-navbar-collapse-1" 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="#">Brand</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li><a href="index.php">Home</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                         <li><a href="#">Menu Item</a></li>
                         <li><a href="#">Menu Item</a></li>
                         <li class="dropdown-submenu">
                             <a tabindex="-1" href="#" class="dropdown-submenu-toggle">Second Dropdown <b class="caret"></b></a>
                             <ul class="dropdown-menu">
                                 <li><a href="#">Sub-Menu Item</a></li>
                                 <li><a href="#">Sub-Menu Item</a></li>
                                 <li><a href="#">Sub-Menu Item</a></li>
                             </ul>
                         </li>
                         <li><a href="#">Menu Item</a></li>
                         <li><a href="#">Menu Item</a></li>
                     </ul>
                 </li>
             </ul>
         </div><!-- /.navbar-collapse -->
      </div><!-- /container -->
    </nav>

现在,让我们来完成大部分繁重的工作,并使用尽可能多的本机Bootstrap类。只需在Bootstrap的主要样式表之后,将此自定义CSS添加到样式表中即可。

/* NEW 2nd-Level Dropdown CSS START */
.dropdown-submenu{position: relative;}
.dropdown-submenu .caret{-webkit-transform: rotate(-90deg); transform: rotate(-90deg);}
.dropdown-submenu > .dropdown-menu {top:0; left:100%; margin-top:-6px; margin-left:-1px;}
.dropdown-submenu.open > a:after{border-left-color:#fff;}
.dropdown-submenu.open > .dropdown-menu, .dropdown-submenu.open > .dropdown-menu {display: block;}
.dropdown-submenu .dropdown-menu{margin-bottom: 8px;}
.navbar-default .navbar-nav .open .dropdown-menu .dropdown-submenu ul{background-color: #f6f6f6;}
.navbar-inverse .navbar-nav .open .dropdown-menu .dropdown-submenu ul{background-color:#333;}
.navbar .navbar-nav .open .dropdown-submenu .dropdown-menu > li > a{padding-left: 30px;}
@media screen and (min-width:992px){
    .dropdown-submenu .dropdown-menu{margin-bottom: 2px;}
    .navbar .navbar-nav .open .dropdown-submenu .dropdown-menu > li > a{padding-left: 25px;}
    .navbar-default .navbar-nav .open .dropdown-menu .dropdown-submenu ul{background-color:#fff;}
    .navbar-inverse .navbar-nav .open .dropdown-menu .dropdown-submenu ul{background-color:#fff;}
} /* END New 2nd-level Dropdown CSS */

CSS占用了我们99%的资源。现在,我们只需要一点Javascript就能把我们带到终点。

// Make Dropdown Submenus possible
$('.dropdown-submenu a.dropdown-submenu-toggle').on("click", function(e){
    $('.dropdown-submenu ul').removeAttr('style');
    $(this).next('ul').toggle();
    e.stopPropagation();
    e.preventDefault();
});
// Clear Submenu Dropdowns on hidden event
$('#bs-navbar-collapse-1').on('hidden.bs.dropdown', function () {
    $('.navbar-nav .dropdown-submenu ul.dropdown-menu').removeAttr('style');
});

就是这样。在http://demo.jdmdigital.co/wp-bootstrap-navwalker-multilevel/wrote a little article上做了一些可下载的现场演示,有关使用WordPress菜单系统时使用相同的代码在Bootstrap菜单中启用此功能。