jQuery 解决方案,在类之间切换,在您的站点中使用 CSS,几乎没有添加,这是一个 jsFiddle http://jsfiddle.net/mdesdev/AVUH4/
HTML
<div id="navi" class="default">
<h1 class="logo"><a href="#">CAUL/CBUA</a></h1>
<ul id="primary-nav">
<li><a href="#">Directories</a></li>
<li><a href="#">Committees</a></li>
<li><a href="#">Resources</a></li>
<li><a href="#">About</a></li>
</ul>
<ul id="tools-nav">
<li class="login"><a href="#">Log In</a></li>
<li class="email icon"><a href="#">Email</a></li>
<li class="twitter icon"><a href="#">Twitter</a></li>
<li class="search icon"><a href="#">Search</a></li>
</ul>
</div>
CSS
#navi {
height: 40px;
width: 961px;
background: #1e416f;
font-family: Verdana, Geneva, sans-serif;
font-size: 13px;
color: white;
text-transform: uppercase;
letter-spacing: 1px;
}
#navi a:hover {
background: white;
color: #1e416f;
}
#navi .logo {
margin: 0;
padding: 0;
float: left;
}
#navi .logo a {
float: left;
width: 56px;
height: 40px;
background: url(/imgs/navi/caul_white_nav.png) center no-repeat;
text-indent: -9999px;
}
#navi .logo a:hover {
background: url(/imgs/navi-hover/caul_blue_nav.png) center no-repeat;
background-color: white;
}
#primary-nav, #tools-nav {
list-style: none;
margin: 0;
padding: 0;
}
#primary-nav li, #primary-nav a, #tools-nav li, #tools-nav a {
float: left;
}
#primary-nav a, #tools-nav a {
color: white;
text-decoration: none;
padding: 0 10px;
border-right: 1px solid white;
line-height: 40px;
}
#tools-nav a:hover {
color: #1e416f;
}
#primary-nav li:first-child a, #tools-nav li:first-child a {
border-left: 1px solid white;
}
#tools-nav {
float: right;
}
#tools-nav .icon a {
text-indent: -9999px;
}
#tools-nav .email a {
background: url(/imgs/navi/mail.png) no-repeat scroll center center transparent;
width: 20px;
}
#tools-nav .email a:hover {
background: url(/imgs/navi-hover/hover_mail.png) no-repeat scroll center center transparent;
width: 20px;
}
#tools-nav .twitter a {
background: url(/imgs/navi/twitter.png) no-repeat scroll center center transparent;
width: 20px;
}
#tools-nav .twitter a:hover {
background: url(/imgs/navi-hover/hover-twitter.png) no-repeat scroll center center transparent;
width: 20px;
}
#tools-nav .search a {
background: url(/imgs/navi/search.png) no-repeat scroll center center transparent;
width: 20px;
}
#tools-nav .search a:hover {
background: url(/imgs/navi-hover/hover_search.png) no-repeat scroll center center transparent;
width: 20px;
}
.default {
margin: 0 0 20px 0;
}
.fixed {
position: fixed;
top: 0;
left: 50%;
margin-left: -480px;
}
jQuery
$(function() {
var navi = $( '#navi' ),
naviOff = navi.offset();
$( window ).scroll(function() {
if($( this ).scrollTop() >= naviOff.top + navi.height() && navi.hasClass( 'default' )) {
navi.removeClass( 'default' ).addClass( 'fixed' ).fadeIn( 'fast' );
}
else if($( this ).scrollTop() <= naviOff.top && navi.hasClass( 'fixed' )) {
navi.removeClass( 'fixed' ).addClass( 'default' ).fadeIn( 'fast' );
}
});
});