Bootstrap中的两个导航栏

时间:2015-09-06 18:11:14

标签: javascript jquery html css twitter-bootstrap

我正在尝试使用Bootstrap做两个导航栏,我在答案中找不到解决方案(虽然有一些已经回答)。我所拥有的是“差不多”工作,但是第二个“导航栏”总是隐藏,一旦我向下滚动,我希望它保持在(和堆叠)第一个(黑色)之前,具有相同的效果和一点点(身高)。

我有一些工作here ...无论如何我也在发布源代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta charset="UTF-8">

    <title>Bootstrap - Two Navigation Bars</title>

    <!-- build:css styles/vendor.css -->
    <!-- bower:css -->
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css">
    <!-- endbower -->
    <!-- endbuild -->

    <!-- build:css styles/main.css -->
    <link rel="stylesheet" href="styles/main.css">
    <!-- endbuild -->

    <!--<link rel="apple-touch-icon" href="apple-touch-icon.png">-->
    <link rel="shortcut icon" href="favicon.ico">
  </head>
  <body data-spy="scroll" data-offset="75">
    <!--[if lt IE 10]>
    <p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">
      upgrade your browser</a> to improve your experience.</p>
    <![endif]-->

    <nav class="navbar navbar-inverse navbar-fixed-top" data-offset-top="80" data-spy="affix">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-target="#top-navbar" data-toggle="collapse"
              aria-expanded="false" aria-controls="navbar">
            <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="#">Project Name</a>
        </div>
        <div id="top-navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Help</a></li>
          </ul>
        </div>
      </div>
    </nav>
    <div class="navbar navbar-default navbar-static-top fixed-element">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".main-nav">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
        </div>
        <div class="collapse navbar-collapse main-nav">
          <ul class="nav navbar-nav">
            <li><a href="#">One</a></li>
            <li class="divider-vertical"></li>
            <li><a href="#about">Two</a></li>
            <li class="divider-vertical"></li>
          </ul>
        </div>
      </div>
    </div>

    <div class="container">
      <div class="jumbotron">
        <h1>'Allo, 'Allo!</h1>
        <p class="lead">Always a pleasure scaffolding your apps</p>
        <p><a class="btn btn-lg btn-success" href="index.html">Splendid!</a></p>
      </div>

      <div class="row marketing">
        <div class="col-lg-6">
          <h4>HTML5 Boilerplate</h4>
          <p>HTML5 Boilerplate is a professional front-end template for building fast, robust, and adaptable web apps or sites.</p>

          <h4>Bootstrap</h4>
          <p>Sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development.</p>
        </div>

        <div class="col-lg-6">
          <h4>HTML5 Boilerplate</h4>
          <p>HTML5 Boilerplate is a professional front-end template for building fast, robust, and adaptable web apps or sites.</p>

          <h4>Bootstrap</h4>
          <p>Sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development.</p>
        </div>

        <div class="col-lg-6">
          <h4>HTML5 Boilerplate</h4>
          <p>HTML5 Boilerplate is a professional front-end template for building fast, robust, and adaptable web apps or sites.</p>

          <h4>Bootstrap</h4>
          <p>Sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development.</p>
        </div>
      </div>

      <hr />

      <footer class="text-center">
        <p>Made with &hearts; from the //shido.io team</p>
      </footer>
    </div>

    <!-- build:js scripts/vendor.js -->
    <!-- bower:js -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <!-- endbower -->
    <!-- endbuild -->

    <!-- build:js scripts/plugins.js -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <!-- endbuild -->

    <!-- build:js scripts/main.js -->
    <!--<script src="scripts/main.js"></script>-->
    <script>
      $(document).ready(function () {
        $('a.page-scroll').bind('click', function (e) { // Ease page scrolling
          var anchor = $(this);

          e.preventDefault();
          $('html, body').stop().animate({
            scrollTop: $(anchor.attr('href')).offset().top
          }, 800, 'swing');
        });
      });
    </script>
    <!-- endbuild -->
  </body>
</html>

...和CSS:

.affix {
  padding: 0;
  -webkit-transition: padding 0.4s linear;
  -moz-transition: padding 0.4s linear;
  -o-transition: padding 0.4s linear;
  transition: padding 0.4s linear;
}

.affix-top {
  padding-top: 6px;
  padding-bottom: 6px;
  -webkit-transition: padding 0.3s linear;
  -moz-transition: padding 0.3s linear;
  -o-transition: padding 0.3s linear;
  transition: padding 0.3s linear;
}

.browserupgrade {
  margin: 0.2em 0;
  background: #cccccc;
  color: #000000;
  padding: 0.2em 0;
}

.container-narrow > hr { margin: 30px 0; }

.dialog {
  bottom: auto;
  margin-left: auto;
  margin-right: auto;
  margin-top: 40px;
  padding: 5px 10px 8px;
  width: 600px; /*60%*/
  resize: both;
}

.footer {
  padding-top: 19px;
  color: #777777;
  border-top: 1px solid #e5e5e5;
}

.footer, .header, .marketing {
  padding-left: 15px;
  padding-right: 15px;
}

.header {
  border-bottom: 1px solid #e5e5e5;
}

.header h3 {
  margin-top: 0;
  margin-bottom: 0;
  line-height: 40px;
  padding-bottom: 19px;
}

.jumbotron {
  text-align: center;
  border-bottom: 1px solid #e5e5e5;
}

.jumbotron .btn {
  font-size: 21px;
  padding: 14px 24px;
}

.marketing { margin: 40px 0; }

.marketing p + h4 { margin-top: 28px; }

.modal-backdrop { background: none; }

.modal-header, .modal-footer { cursor: move; }

.navbar-fixed-top {
  border-bottom: solid 1px #c6cacd;
  background: rgba(0, 0, 0, 0.9);
  box-shadow: 0 1px 4px rgba(49, 49, 64, 0.1);
}

.opener { }

.scrollable-section {
  max-height: 100% /*px*/;
  overflow-y: scroll;
}

@media screen and (min-width: 768px) {
  .container { max-width: 730px; }

  .header { margin-bottom: 30px; }

  .header, .marketing, .footer {
    padding-left: 0;
    padding-right: 0;
  }

  .jumbotron { border-bottom: 0; }
}

body {
  padding-top: 80px;
  padding-bottom: 20px;
}

/* === */

.fixed-element {
  border-bottom: solid 1px #c6cacd;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 1px 4px rgba(49, 49, 64, 0.1);
}

请告诉我遗漏的内容;我认为问题出在.fixed-element选择器中。提前谢谢!

2 个答案:

答案 0 :(得分:1)

navbar-fixed-top添加到两个导航栏并向第二个导航栏添加一个边距,其值等于第一个导航栏的高度,如下所示:

.fixed-element{
    margin-top:63px;
}

然后,将data-affix添加到第二个导航栏。现在,由于滚动时第一个导航栏高度会发生变化,您需要更新第二个导航栏的边距更改,如下所示:

.fixed-element.affix{
    margin-top:51px;
}

这是一个包含上述代码的jsfiddle以及包含的转换:https://jsfiddle.net/AndrewL32/exg6p0bs/1/

答案 1 :(得分:1)

视口缩小后,您可能会遇到导航栏切换问题;这应该解决它。

.browserupgrade {
  margin: 0.2em 0;
  background: #cccccc;
  color: #000000;
  padding: 0.2em 0;
}
.container-narrow > hr {
  margin: 30px 0;
}
.dialog {
  bottom: auto;
  margin-left: auto;
  margin-right: auto;
  margin-top: 40px;
  padding: 5px 10px 8px;
  width: 600px;
  /*60%*/
  resize: both;
}
.footer {
  padding-top: 19px;
  color: #777777;
  border-top: 1px solid #e5e5e5;
}
.footer,
.header,
.marketing {
  padding-left: 15px;
  padding-right: 15px;
}
.header {
  border-bottom: 1px solid #e5e5e5;
}
.header h3 {
  margin-top: 0;
  margin-bottom: 0;
  line-height: 40px;
  padding-bottom: 19px;
}
.jumbotron {
  text-align: center;
  border-bottom: 1px solid #e5e5e5;
}
.jumbotron .btn {
  font-size: 21px;
  padding: 14px 24px;
}
.marketing {
  margin: 40px 0;
}
.marketing p + h4 {
  margin-top: 28px;
}
.modal-backdrop {
  background: none;
}
.modal-header,
.modal-footer {
  cursor: move;
}
.navbar-fixed-top {
  border-bottom: solid 1px #c6cacd;
  background: rgba(0, 0, 0, 0.9);
  box-shadow: 0 1px 4px rgba(49, 49, 64, 0.1);
}
.opener {} .scrollable-section {
  max-height: 100%;
  overflow-y: scroll;
}
@media screen and (min-width: 768px) {
  .container {
    max-width: 730px;
  }
  .header {
    margin-bottom: 30px;
  }
  .header,
  .marketing,
  .footer {
    padding-left: 0;
    padding-right: 0;
  }
  .jumbotron {
    border-bottom: 0;
  }
}
body,
html {
  margin-top: 135px;
  padding-bottom: 20px;
}
.navbar-default.fixed-element {
  border-radius: 0;
  position: fixed;
  top: 60px;
  width: 100%;
  margin-top: 0px;
  border: none;
  border-bottom: solid 1px #c6cacd;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 1px 4px rgba(49, 49, 64, 0.1);
  z-index: 1000;
}
.affix {
  padding: 0;
  -webkit-transition: padding 0.4s linear;
  -moz-transition: padding 0.4s linear;
  -o-transition: padding 0.4s linear;
  transition: padding 0.4s linear;
}
.affix-top {
  padding-top: 6px;
  padding-bottom: 6px;
  -webkit-transition: padding 0.3s linear;
  -moz-transition: padding 0.3s linear;
  -o-transition: padding 0.3s linear;
  transition: padding 0.3s linear;
}
.fixed-element.affix {
  margin-top: -13px;
}
<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.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />

<body data-spy="scroll" data-offset="75">
  <nav class="navbar navbar-inverse navbar-fixed-top" data-offset-top="80" data-spy="affix">
    <div class="container">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-target="#top-navbar" data-toggle="collapse" aria-expanded="false" aria-controls="navbar"> <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="#">Project Name</a>

      </div>
      <div id="top-navbar" class="navbar-collapse collapse">
        <ul class="nav navbar-nav navbar-right">
          <li><a href="#">Help</a>

          </li>
        </ul>
      </div>
    </div>
  </nav>
  <div class="navbar navbar-default fixed-element" data-offset-top="50" data-spy="affix">
    <div class="container">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".main-nav"> <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>

        </button>
      </div>
      <div class="collapse navbar-collapse main-nav">
        <ul class="nav navbar-nav">
          <li><a href="#">One</a>

          </li>
          <li class="divider-vertical"></li>
          <li><a href="#about">Two</a>

          </li>
          <li class="divider-vertical"></li>
        </ul>
      </div>
    </div>
  </div>
  <div class="container">
    <div class="jumbotron">
      <h1>'Allo, 'Allo!</h1>

      <p class="lead">Always a pleasure scaffolding your apps</p>
      <p><a class="btn btn-lg btn-success" href="index.html">Splendid!</a>

      </p>
    </div>
    <div class="row marketing">
      <div class="col-lg-6">
        <h4>HTML5 Boilerplate</h4>

        <p>HTML5 Boilerplate is a professional front-end template for building fast, robust, and adaptable web apps or sites.</p>
        <h4>Bootstrap</h4>

        <p>Sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development.</p>
      </div>
      <div class="col-lg-6">
        <h4>HTML5 Boilerplate</h4>

        <p>HTML5 Boilerplate is a professional front-end template for building fast, robust, and adaptable web apps or sites.</p>
        <h4>Bootstrap</h4>

        <p>Sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development.</p>
      </div>
      <div class="col-lg-6">
        <h4>HTML5 Boilerplate</h4>

        <p>HTML5 Boilerplate is a professional front-end template for building fast, robust, and adaptable web apps or sites.</p>
        <h4>Bootstrap</h4>

        <p>Sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development.</p>
      </div>
    </div>
  </div>
</body>