我正在尝试使用jQuery使我的导航栏坚持到顶部,并且我已经成功了。
当我在本地Web服务器上运行代码时,我得到了我正在寻找的效果。也就是说,在滚动之前具有15px的上边距并且返回滚动,并且在向下滚动时使导航条粘到顶部。但是,当我使用滚轮快速向下滚动时,它看起来并不是无缝的,并且有点不连贯。
我正在使用Bootstrap版本3.3.4和jQuery版本1.11.3。
的index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Example Brand</title>
<link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="/custom/css/index.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-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="/">Example Brand</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="/">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
</nav>
</div>
<div class="container">
<div class="jumbotron">
<span>This is here to add some scrolling space</span>
</div>
<div class="jumbotron">
<span>This is here to add some scrolling space</span>
</div>
<div class="jumbotron">
<span>This is here to add some scrolling space</span>
</div>
<div class="jumbotron">
<span>This is here to add some scrolling space</span>
</div>
<div class="jumbotron">
<span>This is here to add some scrolling space</span>
</div>
<div class="jumbotron">
<span>This is here to add some scrolling space</span>
</div>
<div class="jumbotron">
<span>This is here to add some scrolling space</span>
</div>
<div class="jumbotron">
<span>This is here to add some scrolling space</span>
</div>
<div class="jumbotron">
<span>This is here to add some scrolling space</span>
</div>
<div class="jumbotron">
<span>This is here to add some scrolling space</span>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="/bootstrap/js/bootstrap.min.js"></script>
<script src="/custom/js/index.js"></script>
</body>
</html>
index.js
// On page load function
$(document).ready(function () {
/* Make the top navbar sticky */
// Starting navbar top offset
var navTop = $('.navbar').offset().top;
// On window scroll function
$(window).scroll(function () {
// Scrollbar top offset
var scrollTop = $(window).scrollTop();
// If there is an offset difference
if (scrollTop > navTop) {
// // Set the navbar top offset as the difference
$('.navbar').css('top', scrollTop - navTop);
}
// If there is not an offset difference
else {
// // Set the navbar top offset as 0
$('.navbar').css('top', 0);
}
});
});
index.css
/* Navbar styling */
.navbar {
/* Starting space between navbar and top of page */
/* This space will go away after scrolling down */
margin-top: 15px;
/* High z index so the navbar renders on top */
z-index: 100;
}
此外,here是运行此代码的JsFiddle页面。 Chrome会显示此问题。
我们将非常感谢您提供给我的任何帮助,解决方案或其他建议。谢谢!