简体   繁体   English

将侧边栏链接添加到Bootstrap“汉堡”菜单

[英]Add sidebar links to Bootstrap “hamburger” menu

I have a page made in Bootstrap that has both a top navbar and a sidebar. 我在Bootstrap中创建了一个页面,该页面同时具有顶部导航栏和侧边栏。 Currently, when you resize the page down to a smaller size (or view it on mobile/tablet), the sidebar disappears and the top bar's links go into the "hamburger" menu (the dropdown that is only shown at smaller screen sizes). 当前,当您将页面尺寸缩小至较小尺寸(或在手机/平板电脑上查看)时,侧边栏消失,顶部栏的链接进入“汉堡”菜单(该下拉菜单仅在较小的屏幕尺寸下显示)。 I would like the sidebar's links to be added to the hamburger menu as well, but not to be displayed on the top bar when the screen is large. 我希望边栏的链接也添加到汉堡菜单中,但是当屏幕较大时,不要在顶部栏上显示。

Currently here is what I have for the top bar: 目前,这里是我在顶部栏中显示的内容:

<nav class="navbar navbar-inverse navbar-fixed-top">
(some other stuff)
    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav navbar-right">
        <li class="logout-link"><a href="logout url">Logout</a></li>
      </ul>
    </div>
</nav>

and here is what I have for the side bar (located within a 'row' div): 这是我对侧栏(位于“行” div内)的要求:

    <div class="col-sm-2 col-md-2 sidebar">
      <ul class="nav nav-sidebar">
        <li><a href="fizz">Fizz</a></li>
        <li><a href="buzz">Buzz</a></li>
        <li><a href="blah">Blah</a></li>
      </ul>
    </div>

When you resize the page down, the dropdown menu that appears in the top right looks like this: 调整页面大小时,右上方显示的下拉菜单如下所示:

logout

And I would like it to look like this: 我希望它看起来像这样:

Fizz
Buzz
Blah
======
logout

...without adding "Fizz Buzz Blah" to the top bar of the full-sized screen. ...而没有在全屏顶部添加“ Fizz Buzz Blah”。 How can I do this? 我怎样才能做到这一点? I tried adding the "dropdown" and "nav-navbar" classes to the sidebar column but it didn't work. 我尝试将“下拉列表”和“ nav-navbar”类添加到侧边栏列,但没有用。

Maybe this will help or give you some ideas. 也许这会有所帮助或给您一些想法。

You could do this by separating your nav links with navbar-left / navbar-right and then use navbar-left as your sidebar at widths over 768px. 您可以通过用navbar-left / navbar-right分隔nav链接,然后使用宽度超过768px的navbar-left作为侧边栏来做到这一点。 Then all your links will consolidate inside the mobile navigation under 767px. 然后,您的所有链接都将合并到767px以下的移动导航中。

Note: this setup is for a fullwidth layout (using the container-fluid class), if you're using the container class you'll have to make adjustments for those widths. 注意:此设置适用于全角布局(使用container-fluid类),如果您使用的是container类,则必须对这些宽度进行调整。

See working example Snippet at FullPage. 请参见FullPage上的示例工作片段。

 body { padding-top: 50px; } @media screen and (min-width: 768px) { .sidebar-nav .navbar-nav.navbar-left { top: 50px; bottom: 0; left: 0; width: 150px; position: fixed; background: #222; } .sidebar-nav .navbar-nav.navbar-left > li { display: block; float: none; } .main-content { margin-left: 150px; } } /***FOR DEMO ONLY***/ .main-content .thumbnail { border: none; border-radius: 0; background: rgba(255, 255, 255, 0.75); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.10), 0 1px 2px rgba(0, 0, 0, 0.20); padding: 25px; max-width: 500px; margin: 12.5px auto; } /***FOR DEMO ONLY***/ 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> <div class="wrapper"> <nav class="navbar navbar-inverse navbar-fixed-top sidebar-nav"> <div class="container-fluid"> <div class="navbar-header"> <button data-toggle="collapse" data-target="#bs-nav" type="button" class="navbar-toggle collapsed"><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="navbar-collapse collapse" id="bs-nav"> <ul class="nav navbar-nav navbar-left"> <li><a href="#">Fizz</a> </li> <li><a href="#">Buzz</a> </li> <li><a href="#">Blah</a> </li> </ul> <ul class="nav navbar-nav navbar-right"> <li><a href="#">Logout</a> </li> </ul> </div> </div> </nav> <div class="main-content"> <div class="container-fluid"> <div class="row"> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/000/000" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/f00/f00" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/000/000" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/f00/f00" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/000/000" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/f00/f00" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/000/000" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/f00/f00" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/000/000" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/f00/f00" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/000/000" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/f00/f00" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/000/000" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/f00/f00" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/000/000" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/f00/f00" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/000/000" class="img-responsive" /> </div> </div> <div class="col-lg-2 col-md-4 col-sm-6"> <div class="thumbnail"> <img src="http://placehold.it/450x450/f00/f00" class="img-responsive" /> </div> </div> </div> </div> </div> </div> 

I found an alternate, probably less elegant solution than what vanburen posted, but I'd like to share it in case anyone else has a situation where it would be useful. 我找到了一个替代方法,可能比vanburen发布的解决方案不太优雅,但是我想分享一下,以防其他人遇到有用的情况。

I added "hidden" list items to the list that has the logout link: 我将“隐藏”列表项添加到具有注销链接的列表中:

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav navbar-right">
        <li class="hidden-lg hidden-md hidden-sm dropdown-link"><a href="fizz">Fizz</a></li>
        <li class="hidden-lg hidden-md hidden-sm dropdown-link"><a href="buzz">Buzz</a></li>
        <li class="hidden-lg hidden-md hidden-sm dropdown-link"><a href="blah">Blah</a></li>
        <li class="logout-link"><a href="logout">Logout</a></li>
      </ul>
    </div>

What this does is hide the links if the screen is not 'xs' size, which is the size where the hamburger menu appears. 如果屏幕尺寸不是“ xs”,即汉堡菜单出现的尺寸,这就是隐藏链接。

This is a nice solution if you want items added to your hamburger menu that aren't necessarily found in a different navbar. 如果您希望将不一定要在其他导航栏中找到的项目添加到汉堡菜单,则这是一个很好的解决方案。

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

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