Bootstrap导航栏中的下拉保持打开状态

时间:2018-04-11 19:44:53

标签: css html5 twitter-bootstrap css3 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 {
    display: flex;
    flex-wrap: wrap;
    zoom: 1;
  }
  #myNavbar ul li {
    flex-grow: 1;
  }
  #myNavbar ul li a {
    text-align: left;
  }
}
<head>
  <title>Hello World</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://fonts.googleapis.com/css?family=Work+Sans" rel="stylesheet">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link rel="stylesheet" href="styling.css">

  <script type="text/javascript" src="programming.js"></script>
  <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>
</head>

<body>
  <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>
</body>

enter image description here

2 个答案:

答案 0 :(得分:1)

自定义CSS会始终显示.dropdown-menu,因为display:flex。只需将CSS更改为...

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

演示:https://www.bootply.com/Bev8nG5FYr

答案 1 :(得分:0)

你们有所有的鸭子,但只有一个小姐。您的代码display: flex;#myNavbar ul。所以它显示出来,需要display:none;,点击时必须打开。

所以这就是你需要的:

  1. 只需将display:flex;替换为display: none;,或者只删除display中的#myNavbar ul媒体资源。
  2. 现在,当display:flex作为父级时,将其设为.open。因此,您需要代码为.open > .dropdown-menu { display: flex; }
  3. 以下片段:[#Updated:]

    .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 {
        flex-wrap: wrap;
        zoom: 1;
      }
      
      #myNavbar .open > .dropdown-menu {
        display: flex;
      }
      
      #myNavbar ul li {
        flex-grow: 1;
      }
      #myNavbar ul li a {
        text-align: left;
      }
    }
    <head>
      <title>Hello World</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link href="https://fonts.googleapis.com/css?family=Work+Sans" rel="stylesheet">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <link rel="stylesheet" href="styling.css">
    
      <script type="text/javascript" src="programming.js"></script>
      <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>
    </head>
    
    <body>
      <nav class="navbar navbar-default">
        <div class="container-fluid">
    
          <div class="navbar-header">
            <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>
            <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>
    </body>

    希望这能帮到你!