CSS 调整屏幕大小时引导折叠菜单消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20219796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bootstrap collapse menu disappears when resizing screen
提问by Lucas03
I used bootstrap menu for my app. It looks like this: but when I change window size to smaller, it disappears - looks like this: It disappears when its about ~965px wide (so its probably 979px). I tried this solution, didnt work. May be I'm mixing bootstrap 2 and 3? This is my menu code:
我为我的应用程序使用了引导菜单。它看起来像这样: 但是当我将窗口大小更改为更小时,它消失了 - 看起来像这样: 它在大约 965px 宽(所以它可能是 979px)时消失。我试过这个解决方案,没有用。可能我正在混合引导程序 2 和 3?这是我的菜单代码:
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="/">Digrin</a>
<div class="nav-collapse collapse">
<ul class="nav">
{{ request.path }}
<li{% block nav_stocks %}{% endblock %}><a href="/stocks/">Stocks</a></li>
<li{% block nav_mystocks %}{% endblock %}>
<a href="/stocks/mystocks">My Portfolio</a></li>
<li{% block nav_watchstocks %}{% endblock %}>
<a href="/stocks/watchstocks">Watcher</a></li>
{% if not user.is_authenticated %}
<li class="active"><a href="/accounts/login/?next=/stocks/">Login</a></li>
<li><a href="/accounts/register/">Register</a></li>
{% else %}
<ul class="nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
{{ user.username }}
<i class="caret"></i>
</a>
<ul class="dropdown-menu">
<li><a href="/accounts/password/change/">Change password</a></li>
{% if user.is_staff %}<li><a href="/admin">Admin</a></li>{% endif %}
<li><a href="/accounts/logout/?next=/stocks/">Logout</a></li>
</ul>
</li>
</ul>
{% endif %}
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
includes:
包括:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Le styles -->
<link href="{% static "css/bootstrap.css" %}" rel="stylesheet">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
<link href="{% static "css/bootstrap-responsive.css" %}" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="{% static "js/jquery-1.8.1.min.js" %}" type="text/javascript"></script>
<script src="{% static "js/bootstrap.min.js" %}" type="text/javascript"></script>
And site(far from finished) If I zoom in, menu disappears as well.
和站点(远未完成)如果我放大,菜单也会消失。
回答by Schmalzy
If you do not want your navbar links to be collapsed on smaller devices, then you need to remove the <div class="nav-collapse collapse">
element.
如果您不希望在较小的设备上折叠导航栏链接,则需要删除该<div class="nav-collapse collapse">
元素。
The basic structure should look something like this....
基本结构应该是这样的......
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<a class="brand" href="#">Title</a>
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</div>
</div>
If you want to keep the responsive functionality, then add the below code to within you container
div. This will give you a button to toggle the collapsed menu on smaller devices.
如果您想保留响应功能,请将以下代码添加到您的container
div 中。这将为您提供一个按钮来切换较小设备上的折叠菜单。
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
So then the end result would look something like this.
那么最终的结果将是这样的。
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<!-- Be sure to leave the brand out there if you want it shown -->
<a class="brand" href="#">Project name</a>
<!-- Everything you want hidden at 940px or less, place within here -->
<div class="nav-collapse collapse">
<!-- .nav, .navbar-search, .navbar-form, etc -->
</div>
</div>
</div>
</div>
Also, you do not have to nest a <ul class="nav">
element within a <ul class="nav">
element for the dropdown menus. The correct structure for you navigation items should look like this.
此外,您不必在下拉菜单<ul class="nav">
的<ul class="nav">
元素中嵌套元素。导航项的正确结构应如下所示。
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Test
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
...
</ul>
</li>
</ul>