单击后如何修复闪烁的导航栏

时间:2015-03-18 13:44:56

标签: javascript twitter-bootstrap twitter-bootstrap-3 navbar

我尝试使用此解决方案(Hide Twitter Bootstrap nav collapse on click)在点击时自动隐藏导航折叠。当导航折叠但我在台式机上使用站点时(当导航未折叠时)导航正在闪烁,它正常工作。

代码:

$('.nav a').on('click', function() {
  $(".btn-navbar").click(); //bootstrap 2.x
  $(".navbar-toggle").click() //bootstrap 3.x by Richard
});
.block {
  padding: 70px 0;
}
#section-white {
  background-color: #fff;
  height: 100px;
}
#section-1 {
  margin-top: 51px;
  /* height of header-navbar */
}
#section-1,
#section-2,
#section-3 {
  height: 600px;
  background-color: #555;
  color: #fff;
}
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header page-scroll">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <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 navbar-right">
        <li class="hidden">
          <a class="page-scroll" href="#page-top"></a>
        </li>
        <li>
          <a class="page-scroll" href="#section-1">About</a>
        </li>
        <li>
          <a class="page-scroll" href="#section-2">Services</a>
        </li>
        <li>
          <a class="page-scroll" href="#section-3">Contact</a>
        </li>
      </ul>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container -->
</nav>

<div class="block" id="section-1">
  <div class="container">
    <div class="row">
      Hello World
    </div>
    <!-- /.row -->
  </div>
  <!-- /.container -->
</div>
<!-- /.block -->

<div id="section-white"></div>

<div class="block" id="section-2">
  <div class="container">
    <div class="row">
      Hello World
    </div>
    <!-- /.row -->
  </div>
  <!-- /.container -->
</div>
<!-- /.block -->

<div id="section-white"></div>

<div class="block" id="section-3">
  <div class="container">
    <div class="row">
      Hello World
    </div>
    <!-- /.row -->
  </div>
  <!-- /.container -->
</div>
<!-- /.block -->

您可以在此处试用 - Desktop results,这是源代码 - source code

之前我在Bootstrap 2.3.2中使用过这个解决方案并没有遇到任何问题。

1 个答案:

答案 0 :(得分:0)

试试这个:

JS

$('.nav a').on('click', function() {
    $('.navbar-collapse').collapse('hide');
});

检查更新的 fiddle

同时查看以下答案:Close responsive navbar automatically