Bootstrap下拉菜单在悬停时不单独单独项目

时间:2018-04-12 14:18:59

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

在引导程序导航栏中实施下拉菜单后,出于某种原因,下划线动画不会在下拉列表中为各个项目加下划线 - 它只会强调最底层。应该怎么做才能解决这个问题?



.navbar>navbar-brand {
  color: black;
}

.navbar-default .navbar-nav>li>a {
  color: black;
}

.navbar-default .navbar-nav>li>a:active,
.navbar-default .navbar-nav>li>a:focus {
  background-color: black;
  color: white;
}

.navbar-default ul li a:hover {
  color: #000;
}

.navbar-default ul li a:hover:before {
  width: 100%;
}

.navbar-default ul li a:before {
  content: '';
  display: block;
  position: absolute;
  left: 0;
  bottom: 0;
  height: 3px;
  width: 0;
  background: #000;
  -webkit-transition: width .25s;
  -moz-transition: width .25s;
  -ms-transition: width .25s;
  -o-transition: width .25s;
  transition: width .25s;
}

.navbar-default ul li.last>a:after,
#cssmenu ul li:last-child>a:after {
  display: none;
}

@media screen and (max-width: 768px) {
  .navbar-default ul li {
    float: none;
    width: 100%;
  }
  .navbar-default ul li a {
    width: 100%;
    line-height: 2em;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  .navbar-default ul li a:after {
    display: none;
  }
  .navbar-default ul li a:before {
    height: 1px;
    background: #000;
    width: 100%;
    opacity: .2;
  }
  .navbar-default ul li.last>a:before,
  .navbar-default ul li:last-child>a:before {
    display: none;
  }
}

@media screen and (min-width: 768px) {
  #myNavbar ul:not(.dropdown-menu) {
    display: flex;
    flex-wrap: wrap;
    zoom: 1;
  }
  #myNavbar ul li {
    flex-grow: 1;
  }
  #myNavbar ul li a {
    text-align: left;
  }
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<nav class="navbar navbar-default">
  <div class="container-fluid">

    <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="#">Hello World</a>
    </div>

    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav flex-row">
        <li><a href="#">Link A</a></li>
        <li><a href="#">Link B</a></li>
        <li><a href="#">Link C</a></li>
        <li><a href="#">Link D</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Link E <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Link E.1</a></li>
            <li><a href="#">Link E.2</a></li>
            <li><a href="#">Link E.3</a></li>
            <li><a href="#">Link E.4</a></li>
          </ul>
        </li>
        <li><a href="#">Link F</a></li>
        <li><a href="#">Link G</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Link H <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Link H.1</a></li>
            <li><a href="#">Link H.2</a></li>
          </ul>
        </li>
        <li><a href="#">Link I</a></li>
        <li><a href="#">Link J</a></li>
      </ul>
    </div>
  </div>
</nav>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

此时动画绝对定位到<ul>元素。因此它仅在<ul>元素(bottom:0)的底部可见。您需要将position: relative设置为<li><a>元素,以便动画绝对定位到菜单项。

因此,您可以添加以下CSS来解决问题:

.navbar-default ul li a {
    position:relative;
}

请参阅以下解决方案:

&#13;
&#13;
.navbar>navbar-brand {
  color: black;
}

.navbar-default .navbar-nav>li>a {
  color: black;
}

.navbar-default .navbar-nav>li>a:active,
.navbar-default .navbar-nav>li>a:focus {
  background-color: black;
  color: white;
}
.navbar-default ul li a {
  position:relative;
}
.navbar-default ul li a:hover {
  color: #000;
}

.navbar-default ul li a:hover:before {
  width: 100%;
}

.navbar-default ul li a:before {
  content: '';
  display: block;
  position: absolute;
  left: 0;
  bottom:0;
  height: 3px;
  width: 0;
  background: #000;
  -webkit-transition: width .25s;
  -moz-transition: width .25s;
  -ms-transition: width .25s;
  -o-transition: width .25s;
  transition: width .25s;
}

.navbar-default ul li.last>a:after,
#cssmenu ul li:last-child>a:after {
  display: none;
}

@media screen and (max-width: 768px) {
  .navbar-default ul li {
    float: none;
    width: 100%;
  }
  .navbar-default ul li a {
    width: 100%;
    line-height: 2em;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  .navbar-default ul li a:after {
    display: none;
  }
  .navbar-default ul li a:before {
    height: 1px;
    background: #000;
    width: 100%;
    opacity: .2;
  }
  .navbar-default ul li.last>a:before,
  .navbar-default ul li:last-child>a:before {
    display: none;
  }
}

@media screen and (min-width: 768px) {
  #myNavbar ul:not(.dropdown-menu) {
    display: flex;
    flex-wrap: wrap;
    zoom: 1;
  }
  #myNavbar ul li {
    flex-grow: 1;
  }
  #myNavbar ul li a {
    text-align: left;
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<nav class="navbar navbar-default">
  <div class="container-fluid">

    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#myNavbar" 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="#">Hello World</a>
    </div>

    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav flex-row">
        <li><a href="#">Link A</a></li>
        <li><a href="#">Link B</a></li>
        <li><a href="#">Link C</a></li>
        <li><a href="#">Link D</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Link E <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Link E.1</a></li>
            <li><a href="#">Link E.2</a></li>
            <li><a href="#">Link E.3</a></li>
            <li><a href="#">Link E.4</a></li>
          </ul>
        </li>
        <li><a href="#">Link F</a></li>
        <li><a href="#">Link G</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Link H <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Link H.1</a></li>
            <li><a href="#">Link H.2</a></li>
          </ul>
        </li>
        <li><a href="#">Link I</a></li>
        <li><a href="#">Link J</a></li>
      </ul>
    </div>
  </div>
</nav>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

因为你没有分配位置:相对于下拉列表的各个项目。尝试添加此CSS代码。

.navbar-default ul li {
  position: relative;
}