大家好,所以我试图让效果正常工作是导航栏位于页面中间,用户将向下滚动,因为他点击导航栏它应该粘在页面顶部。例如:here 您可以看到用户向下滚动并点击导航栏时它会粘住并且效果很慢。
我得到了我的工作,但它感觉很笨,因为我的意思是它直接向顶部轻弹,不像示例的那个非常光滑,它很顺利地粘在顶部而我的非常刺耳。
.parallax {
background-color: Black;
min-height: 700px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.parallax1 {
background-color: Red;
min-height: 700px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.navbar {
margin-bottom: 0px;
left: 0;
top: 0;
width: 100%;
list-style: none;
height: 70px;
z-index: 1
}
.navbar-nav {
float: none;
margin: 0;
text-align: center
}
.navbar-nav>li {
float: none;
display: inline-block
}
.navbar-nav>li>a {
line-height: 38px
}
.navbar-nav>li>a.active {
background-color: #E7E7E7
}
.navbar-default .navbar-nav>li>a:hover,
.navbar-default .navbar-nav>li>a:focus {
color: #333333;
background-color: #E7E7E7
}
.navbar-toggle {
background-color: #000000;
background-image: none;
border-radius: 4px;
float: right;
margin-bottom: 8px;
margin-right: 15px;
margin-top: 18px;
padding: 9px 10px;
position: relative
}
.navbar-inverse .navbar-toggle .icon-bar {
background-color: #2DCC70
}
.navbar-default .navbar-toggle:hover,
.navbar-default .navbar-toggle:focus {
background-color: #000000
}
.navbar-inverse .navbar-collapse,
.navbar-inverse .navbar-form {
border-color: transparent
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="parallax"></div>
<nav class="navbar navbar-default" data-spy="affix" data-offset-top="700">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" 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>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#hometab">Home</a></li>
<li><a href="#section1">About Me</a></li>
<li><a href="#section2">My Skills</a></li>
<li><a href="#section3">Portfolio</a></li>
<li><a href="#section4">Contact Me</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>1>
<div class="parallax1"></div>
&#13;
任何帮助都会很棒。
答案 0 :(得分:2)
你只需添加你的风格:
.affix + .parallax1 {
margin-top: 70px;
}
班级.affix
表示导航栏位于顶部。因此,如果.affix
上存在nav
,则必须将边距或填充设置为parallax1
,以便HELLO THERE文本不会隐藏在导航后面。
编辑你的小提琴:
.parallax {
background-color: Black;
min-height: 700px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.parallax1 {
background-color: Red;
min-height: 700px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
/* Add this */
.affix + .parallax1 {
margin-top: 70px;
}
.navbar {
margin-bottom: 0px;
left: 0;
top: 0;
width: 100%;
list-style: none;
height: 70px;
z-index: 1
}
.navbar-nav {
float: none;
margin: 0;
text-align: center
}
.navbar-nav>li {
float: none;
display: inline-block
}
.navbar-nav>li>a {
line-height: 38px
}
.navbar-nav>li>a.active {
background-color: #E7E7E7
}
.navbar-default .navbar-nav>li>a:hover,
.navbar-default .navbar-nav>li>a:focus {
color: #333333;
background-color: #E7E7E7
}
.navbar-toggle {
background-color: #000000;
background-image: none;
border-radius: 4px;
float: right;
margin-bottom: 8px;
margin-right: 15px;
margin-top: 18px;
padding: 9px 10px;
position: relative
}
.navbar-inverse .navbar-toggle .icon-bar {
background-color: #2DCC70
}
.navbar-default .navbar-toggle:hover,
.navbar-default .navbar-toggle:focus {
background-color: #000000
}
.navbar-inverse .navbar-collapse,
.navbar-inverse .navbar-form {
border-color: transparent
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<div class="parallax">
</div>
<nav class="navbar navbar-default" data-spy="affix" data-offset-top="700">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" 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>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#hometab">Home</a></li>
<li><a href="#section1">About Me</a></li>
<li><a href="#section2">My Skills</a></li>
<li><a href="#section3">Portfolio</a></li>
<li><a href="#section4">Contact Me</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<div class="parallax1">
<h1>
HELLO THERE
</h1>
</div>
&#13;
答案 1 :(得分:1)
所需示例如此流畅的原因是因为它使用了smoothScroll库。我只是将它包含在您提供的示例小提琴中,它的工作方式与您展示的演示网站非常相似,请查看 here 。您不需要添加任何代码,因为一旦页面加载完成后库已经初始化:
addEvent(wheelEvent, wheel);
addEvent('mousedown', mousedown);
addEvent('load', init);
如果您有任何其他问题,请告诉我们。)
答案 2 :(得分:1)
平滑粘性导航栏它正在使用jquery 2.希望这适用于您的测试。在所有屏幕尺寸上跟踪导航栏。
var navOffset, scrollPos = 0;
function sticky() {
if (!$(".navbar").hasClass("fixed")) {
navOffset = $(".navbar").offset().top;
}
}
sticky();
$(window).scroll(function() {
scrollPos = $(window).scrollTop();
if (scrollPos >= navOffset) {
var ht = $(".navbar").outerHeight() + 10;
$(".navbar").addClass("navbar-fixed-top");
$(".nav-placeholder").css({
'height': ht + "px",
'display': 'block'
});
} else {
$(".navbar").removeClass("navbar-fixed-top");
$(".nav-placeholder").css({
'height': 0,
'display': 'none'
});
}
});
.parallax {
background-color: Black;
min-height: 700px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.parallax1 {
background-color: Red;
min-height: 700px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.navbar {
margin-bottom: 0px;
left: 0;
top: 0;
width: 100%;
list-style: none;
height: 70px;
z-index: 1
}
.navbar-nav {
float: none;
margin: 0;
text-align: center
}
.navbar-nav>li {
float: none;
display: inline-block
}
.navbar-nav>li>a {
line-height: 38px
}
.navbar-nav>li>a.active {
background-color: #E7E7E7
}
.navbar-default .navbar-nav>li>a:hover,
.navbar-default .navbar-nav>li>a:focus {
color: #333333;
background-color: #E7E7E7
}
.navbar-toggle {
background-color: #000000;
background-image: none;
border-radius: 4px;
float: right;
margin-bottom: 8px;
margin-right: 15px;
margin-top: 18px;
padding: 9px 10px;
position: relative
}
.navbar-inverse .navbar-toggle .icon-bar {
background-color: #2DCC70
}
.navbar-default .navbar-toggle:hover,
.navbar-default .navbar-toggle:focus {
background-color: #000000
}
.navbar-inverse .navbar-collapse,
.navbar-inverse .navbar-form {
border-color: transparent
}
.nav-placeholder {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="parallax"></div>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" 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>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#hometab">Home</a></li>
<li><a href="#section1">About Me</a></li>
<li><a href="#section2">My Skills</a></li>
<li><a href="#section3">Portfolio</a></li>
<li><a href="#section4">Contact Me</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<div class="nav-placeholder"></div>
<p>Some text for test</p>
<div class="parallax1"></div>
答案 3 :(得分:0)
使用bootstrap Class&#34; navbar-fixed-top&#34;
<nav class="navbar navbar-default navbar-fixed-top" data-spy="affix" data-offset-top="700">
答案 4 :(得分:0)
请遵循此代码,将附加值设置为顶部的导航间隙
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.affix {
top: 0;
width: 100%;
}
.affix + .container-fluid {
padding-top: 70px;
}
</style>
</head>
<body>
<div class="container-fluid" style="background-color:#000;color:#fff;height:200px;">
Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"
</div>
<nav class="navbar navbar-inverse" data-spy="affix" data-offset-top="197">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Basic Topnav</a></li>
<li><a href="#">Page 1</a></li>
<li><a href="#">Page 2</a></li>
<li><a href="#">Page 3</a></li>
</ul>
</nav>
<div class="container-fluid" style="height:1000px">
<p>
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
</p>
<p>
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
</p>
</div>
</body>
</html>