Bootstrap 导航栏故障排除:意外的下拉菜单行为

Bootstrap Navbar troubleshooting: unexpected drop down menu behavior

我在这里遇到了一些问题,但主要问题是下拉菜单的移动响应能力。我不知道我做错了什么。

这是我的 HTML:

<div class="navbar navbar-inverse">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
        </div><!--nav-header-->
        <div class="collapse navbar-collapse">
            <ul class="nav nav-pills">
                <li><a href="index.html">Home</a></li>
                <li><a href="About.html">About</a></li>
                <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" data-target="#">Practice <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">A</a></li>
                        <li><a href="#">B</a></li>
                        <li><a href="#">C</a></li>
                        <li><a href="#">D</a></li>
                        <li><a href="#">E</a></li>
                    </ul>
                </li>
                <li><a href="Blog.html">Blog</a></li>
                <li><a href="Contact.html">Contact</a></li>
            </ul>
        </div><!--collapse-->
    </div><!--container-->
</div><!--navbar-->

这是我的 CSS:

.navbar {
  background: black;
  position: absolute;
  margin-top: 50px;
  width: 100%;
  border-top: 2px solid black;
  border-bottom: 3px solid #4f3838;}
.navbar .nav-pills li {
  font-size: 1.2em !important;}
.navbar .nav-pills a {
  color: white;
  margin: 0 3px 0 3px;}
.navbar .nav-pills a:hover {
  color: black;}
.dropdown .dropdown-menu {
  background: black;}

我在移动设备上遇到两个问题:首先,在我点击然后取消点击菜单按钮后,它保持 selected(点击后它显示为灰色而不是黑色),然后是主要的下拉菜单水平显示,下拉菜单中的下拉菜单垂直显示,造成混乱。我不知道我做了什么导致这个或如何解决它。当我查看教程时,示例导航栏与我的基本相同,并且它们的下拉菜单表现符合预期。

我的最后一个问题是,当我 select 下拉菜单 "practice" 我将文本设置为在鼠标悬停时变黑,但是因为它是导航栏,所以当它悬停时背景是白色的selected/open。因此,如果您打开菜单并将鼠标移开,则文本为白色,背景也为白色。是否有 CSS 解决方案?

您可以在此处查看代码:http://www.bootply.com/5q1qGfp2c3

如果您转到移动视图并向下滚动 window,您就会明白我所说的下拉菜单的意思。

将脚本链接添加到最新的 Bootstrap 和 jQuery JavaScript。这应该可以修复您的移动宽度导航栏下拉菜单。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

您看到奇怪的行为是因为您使用的是 nav-pills instead of the default navbar 元素。请参阅使用一些方法自定义所有内容的工作示例。

/***Navbar Background Color, Border Removed ,Border Radius Sqaure***/

.navbar.navbar-custom {
  background: black;
  margin-top: 50px;
  border: none;
  border-top: 2px solid black;
  border-bottom: 3px solid #4f3838;
  border-radius: 0;
}
/***Link Color***/

.navbar.navbar-custom .navbar-nav > li > a {
  color: white;
  margin: 0 3px 0 3px;
  font-size: 1.2em;
}
/***Link Color Hover Statr***/

.navbar.navbar-custom .navbar-nav > li > a:hover {
  color: black;
}
/***Link Color Hover Statr***/

.navbar.navbar-custom .navbar-nav > li:hover,
.navbar.navbar-custom .navbar-nav > li:focus {
  background: white;
}
/***Link Background and Color Active State***/

.navbar.navbar-custom .navbar-nav > .active,
.navbar.navbar-custom .navbar-nav > .active > a,
.navbar.navbar-custom .navbar-nav > .active > a:hover,
.navbar.navbar-custom .navbar-nav > .active > a:focus {
  background: white;
  color: black;
}
/***Navbar Brand Link Color***/

.navbar.navbar-custom .navbar-brand {
  color: white;
}
/***Dropdown-menu Background Color***/

.navbar.navbar-custom .dropdown-menu {
  background: black;
  border: none;
}
/***Dropdown-menu Color***/

.navbar.navbar-custom .dropdown-menu > li > a {
  color: white;
  font-size: 1.2em;
}
/***Dropdown-menu Color Hover and Focus State***/

.navbar.navbar-custom .dropdown-menu > li > a:hover,
.navbar.navbar-custom .dropdown-menu > li > a:focus {
  color: black;
  background: white;
}
/***Dropdown Background Active State***/

.navbar.navbar-custom .nav li.dropdown.open > .dropdown-toggle,
.navbar.navbar-custom .nav li.dropdown.active > .dropdown-toggle,
.navbar.navbar-custom .nav li.dropdown.open.active > .dropdown-toggle {
  background: white;
  color: black;
}
/***Toggle Button***/

.navbar.navbar-custom .navbar-header .navbar-toggle {
  border: none;
}
/***Toggle Button Hover and Focus State***/

.navbar.navbar-custom .navbar-header .navbar-toggle:hover,
.navbar.navbar-custom .navbar-header .navbar-toggle:focus {
  background: white;
  border: none;
}
/***Collapse Borders***/

.navbar.navbar-custom .navbar-collapse {
  border: none;
}
@media (max-width: 767px) {
  .navbar.navbar-custom li.dropdown .dropdown-menu > li > a {
    color: white;
    background: black;
  }
  /***Dropdown-menu Color Hover and Focus State***/
  .navbar.navbar-custom li.dropdown .dropdown-menu > li > a:hover,
  .navbar.navbar-custom li.dropdown .dropdown-menu > li > a:focus {
    color: black;
    background: white;
  }
}
<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.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default navbar-custom">
  <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">
      <ul class="nav navbar-nav">
        <li class="active"><a href="index.html">Home</a>

        </li>
        <li><a href="About.html">About</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="#">A</a>

            </li>
            <li><a href="#">B</a>

            </li>
            <li><a href="#">C</a>

            </li>
            <li><a href="#">D</a>

            </li>
            <li><a href="#">E</a>

            </li>
          </ul>
        </li>
        <li><a href="Blog.html">Blog</a>

        </li>
        <li><a href="Contact.html">Contact</a>

        </li>
      </ul>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container-fluid -->
</nav>