单击后如何修复闪烁的导航栏
How to fix blinking navbar after click
我尝试使用此解决方案 (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 and this is source code - source code
早些时候我在 Bootstrap 2.3.2 中使用了这个解决方案并且没有遇到任何问题。
试试这个:
JS
$('.nav a').on('click', function() {
$('.navbar-collapse').collapse('hide');
});
检查更新后的 fiddle。
同时检查这个答案:Close responsive navbar automatically
我尝试使用此解决方案 (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 and this is source code - source code
早些时候我在 Bootstrap 2.3.2 中使用了这个解决方案并且没有遇到任何问题。
试试这个:
JS
$('.nav a').on('click', function() {
$('.navbar-collapse').collapse('hide');
});
检查更新后的 fiddle。
同时检查这个答案:Close responsive navbar automatically