<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>JS之网页定位导航特效</title>
<style>
*{
padding: 0;
margin: 0;
}
body{
font-family: "Microsoft YaHei light", "Microsoft YaHei";
font-size: 16px;
line-height: 1.7;
}
li{
list-style: none;
}
#menu{
position: fixed;
left: 50%;
top: 200px;
margin-left: 400px;
width: 80px;
}
#menu ul li a{
display: block;
margin: 5px 0;
font-weight: bold;
font-size: 14px;
color: #333;
width: 80px;
height: 50px;
line-height: 50px;
text-decoration: none;
text-align: center;
}
#menu ul li a:hover,
#menu ul li a.current{
color: #fff;
background-color: #0088bb;
}
#content{
width: 800px;
margin: 0 auto;
}
#content h1{
color: #0088bb;
text-align: center;
}
#content .item{
padding: 20px;
margin-bottom: 20px;
border: 1px dotted #999999;
}
#content .item h2{
font-size: 16px;
font-weight: bold;
border-bottom: 2px solid #0088bb;
margin-bottom: 10px;
}
#content .item li{
display: inline;
margin-right: 10px;
}
#content .item li a img{
width: 230px;
height: 230px;
}
</style>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
window.onload = function () {
$(window).scroll(function () {
var top = $(document).scrollTop;
var menu = $('#menu');
var items = $('#content').find('.item');
var currentId = '';
items.each(function () {
var that = $(this);
if(top > that.offset().top-200){
currentId = '#'+that.attr('id');
}else{
return false;
}
});
var currentLink = menu.find('.current');
if(currentId && currentLink.attr('href') != currentId){
currentLink.removeClass('current');
menu.find('[href='+currentId+']').addClass('current');
}
});
};
</script>
</head>
<body>
<div id="menu">
<ul>
<li><a href="#item1" class="current">F1 女装</a></li>
<li><a href="#item2">F2 男装</a></li>
<li><a href="#item3">F3 美妆</a></li>
<li><a href="#item4">F4 母婴</a></li>
<li><a href="#item5">F5 数码</a></li>
</ul>
</div>
<div id="content">
<h1>购物网</h1>
<div id="item1" class="item">
<h2>F1 女装</h2>
<ul>
<li><a href="#"><img src="../images/1.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/1.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/1.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/1.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/1.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/1.jpg" alt=""/></a></li>
</ul>
</div>
<div id="item2" class="item">
<h2>F2 男装</h2>
<ul>
<li><a href="#"><img src="../images/2.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/2.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/2.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/2.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/2.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/2.jpg" alt=""/></a></li>
</ul>
</div>
<div id="item3" class="item">
<h2>F3 美妆</h2>
<ul>
<li><a href="#"><img src="../images/3.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/3.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/3.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/3.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/3.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/3.jpg" alt=""/></a></li>
</ul>
</div>
<div id="item4" class="item">
<h2>F4 母婴</h2>
<ul>
<li><a href="#"><img src="../images/4.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/4.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/4.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/4.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/4.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/4.jpg" alt=""/></a></li>
</ul>
</div>
<div id="item5" class="item">
<h2>F5 数码</h2>
<ul>
<li><a href="#"><img src="../images/5.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/5.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/5.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/5.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/5.jpg" alt=""/></a></li>
<li><a href="#"><img src="../images/5.jpg" alt=""/></a></li>
</ul>
</div>
</div>
</body>
</html>
var top = $(document).scrollTop;这里少了个括号,应该是 var top = $(document).scrollTop();