具有可折叠和不可折叠元素的Navbar

时间:2015-07-25 16:36:06

标签: javascript jquery html css twitter-bootstrap

我需要在bootstrap中创建一个导航栏,在桌面上显示如下图像。 (图1)

enter image description here

在移动设备上,我希望将其折叠,如图2草图所示。

enter image description here

为此我使用了bootstrap的代码如下。

<div>

<nav class="navbar navbar-default navbar-fixed-top">

  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span> 
      </button>
      <a class="navbar-brand" href="#">WebSiteName</a>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#">Page 1</a></li>
        <li><a href="#">Page 2</a></li> 
        <li><a href="#">Page 3</a></li> 
      </ul>

    </div>
    <div>
        <ul class="nav navbar-nav navbar-right">
        <li><a href="#"><span class="glyphicon glyphicon-user"></span> My Profile</a></li>
        <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Post</a></li>
      </ul>
    </div>
  </div>

</nav>

</div>

当屏幕尺寸缩小时,移动视图中的图像就是我想要的,因此只显示字形。

因为我是新手,请让我知道你的想法。 JsFiddle is here.

1 个答案:

答案 0 :(得分:2)

这是实现此目的的一种方法示例:将nav重组为列,以便考虑修复的正确图标。

.wrapper {
  padding-left: 15px;
  padding-right: 15px;
  padding-bottom: 75px;
  margin: 0 auto;
}
.wrapper .navbar-default {
  border: transparent;
  margin: 0px;
  padding: 0px;
  width: 100%;
  border-bottom: 6px solid #266080;
  height: 56px;
  position: fixed;
  border-radius: 0px;
}
.navbar-toggle {
  right: 35%;
}
.navbar-collapse {
  background: #f5f5f5;
}
.no-gutter >[class*='col-'] {
  padding-right: 0;
  padding-left: 0;
}
.login {
  display: inline;
  position: fixed;
  width: 100%;
  height: 56px;
  margin: 0px;
  padding: 0px 0px 0px 25px;
  border: transparent;
  border-radius: 0px;
}
.login .glyphicon {
  padding: 10px 5px;
  margin: 0 auto;
}
.login p {
  display: inline;
  padding: 0;
}
.main-content {
  padding: 0 50px;
  margin: 0 auto;
}
@media (max-width: 768px) {
  .login .glyphicon {
    padding: 10px 0px;
  }
  .login {
    padding: 0;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="wrapper">
  <div class="row no-gutter">
    <div class="col-xs-9 col-xs-8">
      <nav class="navbar navbar-default">
        <!-- 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="#">Brand</a> 
        </div>
        <!-- Collect the nav links, forms, 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 -->
      </nav>
      <!--end nav-->
    </div>
    <!--end col9-->
    <div class="col-xs-3 col-xs-4">
      <div class="login">
        <a href="#" class="btn btn-nav"> <span class="glyphicon glyphicon-user"></span>
          <p class="hidden-xs">Profile</p>
        </a>
        <a href="#" class="btn btn-nav"> <span class="glyphicon glyphicon-log-in"></span>
          <p class="hidden-xs">Post</p>
        </a>
      </div>
    </div>
    <!--end col3-->
  </div>
  <!--end row-->
</div>
<!--end wrapper-->
<div class="main-content">
  <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
    one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum
    et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section
    1.10.32.
  </p>
  <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
    one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum
    et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section
    1.10.32.
  </p>
  <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
    one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum
    et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section
    1.10.32.
  </p>
  <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
    one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum
    et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section
    1.10.32.
  </p>
</div>