![](/img/trans.png)
[英]How to create a sticky navigation bar that becomes fixed to the top after scrolling
[英]Fixed navigation bar after scrolling
我正在尝试创建一个导航栏,该导航栏只有在到达页面顶部后才保持固定。 我的代码可以正常工作,因此导航是固定的,但是我似乎无法让它首先滚动到顶部。
这是HTML:
<div id= "home"> contentcontentcontent </div>
<div id="nav">
<a href="#home">home</a>
<a href="#gogreen">go green</a>
<a href="#yourarea">your area</a>
<a href="#howto">how to</a></div>
和CSS:
nav {
text-align: center;
top: 600;
z-index: 100;
position: fixed;
width: 100%;
border: 0;
margin-bottom: 0;}
fixed {
top:600;
z-index: 100;
position: fixed;
width: 100%;}
home {
overflow: hidden;}
和jQuery:
$(document).ready(function() {
$(window).scroll(function () {
//console log determines when nav is fixed
console.log($(window).scrollTop())
if ($(window).scrollTop() > 600) {
$('#nav').addClass('fixed');
}
if ($(window).scrollTop() < 601) {
$('#nav').removeClass('fixed');
}
});
这些是基于对该站点上类似问题的回答,但是到目前为止,似乎没有任何效果。 有人知道我的代码有什么问题吗?
编写CSS选择器时,id和类必须以#
或a为前缀.
分别。 在您的CSS中
nav { // rules }
fixed { // rules }
home { // rules }
什么时候应该有
#nav { // rules }
.fixed { // rules }
#home { // rules }
这是您代码工作的小提琴。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.