如何使用2折叠菜单创建导航栏?

时间:2017-05-03 12:35:35

标签: html css twitter-bootstrap

我的代码在移动视图上工作正常但在桌面右侧链接正在离开导航栏。我正在尝试创建导航栏,其中Brand位于中心,搜索栏位于左侧,链接位于右侧。以及如何在左侧移动hambuger栏并在移动视图中右侧搜索..(对不起我的英语)

.navbar-brand {
    position: absolute;
    width: 100%;
    left: 0;
    top: 0;
    text-align: center;
    margin: auto;
}
.navbar-toggle {
    z-index:3;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>


<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-2">
        <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>
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-1"><span class="glyphicon glyphicon-search" aria-hidden="true"> </span>
      </button>
    </div>
       <div class="collapse navbar-collapse navbar-left" id="navbar-collapse-1">
     <form class="navbar-form navbar-left" role="search">
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Search">
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
     </form>
     </div>
      <div class="collapse navbar-collapse" id="navbar-collapse-2">
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
      </ul>
    </div>
</nav>

2 个答案:

答案 0 :(得分:1)

&#13;
&#13;
@media (max-width: 768px) {
 .nav_bar_align .navbar-brand{
    position: absolute;
    width: 100%;
    left: 0;
    top: 0;
    text-align: center;
    margin: auto;
 }
 .nav_bar_align .nav_humber{
    float: left;
    margin-left: 15px;
    z-index: 1;
 }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>


<nav class="navbar navbar-default navbar-fixed-top nav_bar_align" role="navigation">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle nav_humber" data-toggle="collapse" data-target="#navbar-collapse-2">
        <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>
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-1"><span class="glyphicon glyphicon-search" aria-hidden="true"> </span>
      </button>
    </div>
       <div class="collapse navbar-collapse navbar-left" id="navbar-collapse-1">
     <form class="navbar-form navbar-left" role="search">
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Search">
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
     </form>
     </div>
      <div class="collapse navbar-collapse" id="navbar-collapse-2">
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
      </ul>
    </div>
</nav>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

添加menu-1课程,或者将其调用到button

 <button type="button" class="navbar-toggle menu-1" data-toggle="collapse" data-target="#navbar-collapse-2">

然后将此添加到您的css:

.menu-1 {
float: left;
margin-left: 10px;
}

更新:处理搜索问题,删除width: 100并为left增加.navbar-brand,如下所示:

.navbar-brand {
    position: absolute;
    /*width: 100%;*/
    left: 50%;
    top: 0;
    text-align: center;
    margin: auto;
}

并添加此css:

@media (max-width:767px){
    a.navbar-brand {
        left: 45%;
  }
}

检查更新后的Jsfiddle