折叠时在导航栏上保持搜索

时间:2016-11-01 12:10:08

标签: css twitter-bootstrap twitter

当菜单折叠时,我试图在我的导航栏右侧维护一个搜索栏,但似乎无法按照我想要的方式获得它。

我希望在右侧搜索完整菜单,如下所示: full menu

它的崩溃看起来像这样: collapsed menu

我能达到的最好效果是将搜索对话框包装到下一行并且看起来很糟糕。

1 个答案:

答案 0 :(得分:2)

要实现此目的,您需要从<form>中取出.navbar-collapse并使用@media查询将搜索框放在您想要的位置。

@media (min-width: 320px) and (max-width: 899px) {
  .navbar-search {
    padding: 0;
    position: absolute;
    top: 0;
    right: 100px;
  }
}

@media (min-width: 900px) {
  .navbar-search {
    position: absolute;
    top: 0;
    right: 40px;
  }
}
<nav class="navbar navbar-default">
  <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="#">Logo</a>
    </div>

    <!-- Collect the nav links 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="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</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="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
    </div>
    <!-- /.navbar-collapse -->
    <!-- Search form goes here out side of .navbar-collapse -->
    <form class="navbar-form navbar-search">
      <div class="form-group">
        <input type="text" class="form-control" placeholder="Search">
      </div>
      <button type="submit" class="btn btn-default hidden-xs hidden-sm">Submit</button>
    </form>
  </div>

  <!-- /.container-fluid -->
</nav>

这是一个工作的codepen示例供您查看。 http://codepen.io/yongchuc/pen/dpxeEg