删除导航栏可切换后隐藏的导航栏项目

时间:2017-01-17 17:35:03

标签: jquery html css twitter-bootstrap

enter image description here我正在使用Bootstrap处理导航栏。当屏幕大于990分辨率时,我需要屏幕上的toggleable按钮和nav-items。所以我尝试从navbar-toggleable-md移除nav类,这样做会给我带来按钮,但会隐藏nav-items

有没有办法显示可切换按钮和导航项?

 <nav class="navbar navbar-light bg-faded">
    <button class="navbar-toggler navbar-toggler-left" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="#">Navbar</a>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active"> <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> </li>
            <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li>
            <li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li>
        </ul>
        <form class="form-inline my-2 my-lg-0">
            <input class="form-control mr-sm-2" type="text" placeholder="Search">
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
    </div>
</nav>

1 个答案:

答案 0 :(得分:0)

这里有一个简单的例子,你可以从这样的事情开始:

HTML:

<nav class="navbar navbar-toggleable-md navbar-light bg-faded my-navbar">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">Navbar</a>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
  </div>
</nav>

CSS:

@media (min-width: 992px) {
  .my-navbar.navbar-toggleable-md .navbar-toggler {
    display: block;
  }
  .my-navbar.navbar-toggleable-md .navbar-collapse {
    display: none !important;
    transition: none;
  }
  .my-navbar.my-navbar-opened.navbar-toggleable-md .navbar-collapse {
    display: -webkit-box!important;
    display: -webkit-flex!important;
    display: -ms-flexbox!important;
    display: flex!important;
  }
}

JS:

$('.navbar-toggler').click(function() {
  $('.my-navbar').toggleClass('my-navbar-opened')
});

CODEPEN