过去几天我一直在学习bootstrap 3和sass。无论如何,当屏幕尺寸低于1000px时,我试图制作一个折叠的导航栏。它有效,但当我点击切换按钮,显示菜单下拉菜单时,它会打开,然后立即关闭。
以下是代码:http://jsbin.com/zahatekisa/edit?html,css,output
.navbar {
background-color: #202C39;
}
div.navbar-header a.navbar-brand {
font-family: "Caveat Brush";
margin-right: 20px;
font-size: 23px;
color: #F29559;
}
div.navbar-header a.navbar-brand:hover {
color: #ffb36b;
}
ul.nav.navbar-nav li a {
font-family: "PT Sans", "cursive";
color: #e6e6e6;
}
ul.nav.navbar-nav li a:hover {
color: #ffffff;
background-color: #2a394a;
}
ul.navbar-right li p.navbar-text {
display: block;
}
@media (max-width: 1050px) {
ul.navbar-right li a {
float: left;
}
ul.navbar-right li p.navbar-text {
margin-left: 15px;
position: relative;
top: -4px;
}
}
@media (max-width: 1000px) {
.navbar-header {
float: none;
}
.navbar-toggle {
display: block;
}
.navbar-collapse {
border-top: 1px solid transparent;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
}
.navbar-collapse.collapse {
display: none !important;
}
.navbar-nav {
float: none !important;
margin: 7.5px -15px;
}
.navbar-nav > li {
float: none;
}
.navbar-nav > li > a {
padding-top: 10px;
padding-bottom: 10px;
}
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.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"/>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<!--Making the toggle button-->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!--Logo-->
<a href="#" class="navbar-brand">Lorem ipsum dolor.</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><a href="#">Lorem</a>
</li>
<li><a href="#">Lorem</a>
</li>
<li><a href="#">Lorem</a>
</li>
<li><a href="#">Lorem</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<p class="navbar-text">Contact Me:</p>
</li>
<li><a href="#"><i class="fa fa-facebook-official" aria-hidden="true"></i></a>
</li>
<li><a href="#"><i class="fa fa-twitter-square" aria-hidden="true"></i></a>
</li>
<li><a href="#"><i class="fa fa-skype" aria-hidden="true"></i></a>
</li>
<li><a href="#"><i class="fa fa-envelope" aria-hidden="true"></i></a>
</li>
</ul>
</div>
</div>
</nav>
&#13;
感谢任何帮助。
答案 0 :(得分:6)
这里的问题是你拥有的这种特殊风格,它强制导航栏被隐藏,无论它是打开还是关闭:
@media (max-width: 1000px) {
.navbar-collapse.collapse {
display: none !important;
}
}
我可以看到您在此处所做的事情 - 您正在尝试将Bootstrap的.collapse
课程与您自己的自定义添加功能合并到导航栏中。但是,您的逻辑存在问题:我怀疑您是在假设Bootstrap切换.collapse
隐藏或显示折叠内容的情况下编写您的样式的。这是不正确的。实际上,Bootstrap会为此切换.in
- 因此,如果元素具有类.in
,则不应隐藏内容。只需将选择器更改为:
@media (max-width: 1000px) {
.navbar-collapse.collapse:not(.in) {
display: none !important;
}
}
这里是上下文中的修复:
.navbar {
background-color: #202C39;
}
div.navbar-header a.navbar-brand {
font-family: "Caveat Brush";
margin-right: 20px;
font-size: 23px;
color: #F29559;
}
div.navbar-header a.navbar-brand:hover {
color: #ffb36b;
}
ul.nav.navbar-nav li a {
font-family: "PT Sans", "cursive";
color: #e6e6e6;
}
ul.nav.navbar-nav li a:hover {
color: #ffffff;
background-color: #2a394a;
}
ul.navbar-right li p.navbar-text {
display: block;
}
@media (max-width: 1050px) {
ul.navbar-right li a {
float: left;
}
ul.navbar-right li p.navbar-text {
margin-left: 15px;
position: relative;
top: -4px;
}
}
@media (max-width: 1000px) {
.navbar-header {
float: none;
}
.navbar-toggle {
display: block;
}
.navbar-collapse {
border-top: 1px solid transparent;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
}
.navbar-collapse.collapse:not(.in) {
display: none !important;
}
.navbar-nav {
float: none !important;
margin: 7.5px -15px;
}
.navbar-nav > li {
float: none;
}
.navbar-nav > li > a {
padding-top: 10px;
padding-bottom: 10px;
}
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.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"/>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<!--Making the toggle button-->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!--Logo-->
<a href="#" class="navbar-brand">Lorem ipsum dolor.</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><a href="#">Lorem</a>
</li>
<li><a href="#">Lorem</a>
</li>
<li><a href="#">Lorem</a>
</li>
<li><a href="#">Lorem</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<p class="navbar-text">Contact Me:</p>
</li>
<li><a href="#"><i class="fa fa-facebook-official" aria-hidden="true"></i></a>
</li>
<li><a href="#"><i class="fa fa-twitter-square" aria-hidden="true"></i></a>
</li>
<li><a href="#"><i class="fa fa-skype" aria-hidden="true"></i></a>
</li>
<li><a href="#"><i class="fa fa-envelope" aria-hidden="true"></i></a>
</li>
</ul>
</div>
</div>
</nav>
&#13;
(还有一点值得注意的是,您可能希望使用max-width: 768px
而非max-width: 1000px
进行媒体查询,因为这是&#34;平板电脑&#34;的引导断点。大小的设备。)
希望这有帮助!如果您有任何问题,请告诉我。