码迷,mamicode.com
首页 > Web开发 > 详细

网页定位导航

时间:2014-12-23 23:57:50      阅读:456      评论:0      收藏:0      [点我收藏+]

标签:

<style>
    #header{height:50px; }
    #siderList{position: fixed; right:0; bottom: 20%;}
    #siderList li{margin-bottom: 5px; list-style: none;}
    #siderList li a{display: inline-block; width: 50px; height: 50px; background: #32B981; text-align: center; color: #fff; line-height: 50px; cursor: pointer;}
    #siderList li a.active{background: #4390ee;}
    #item1{height: 400px; background: #008451;}
    #item2{height: 1000px; background: #808080;}
    #item3{height:500px; background: #973e76;}
    #item4{height:500px; background: #2489ce;}
</style>
<div id="header">导航头</div>
<div id="list">
    <div id="item1" class="item">
        1f
    </div>
    <div id="item2" class="item">
        2f
    </div>
    <div id="item3" class="item">
        3f
    </div>
    <div id="item4" class="item">
        4f
    </div>
</div>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<ul id="siderList">
    <li><a href="#item1">1F</a></li>
    <li><a href="#item2">2F</a></li>
    <li><a href="#item3">3F</a></li>
    <li><a href="#item4">4F</a></li>
</ul>

<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
<script>
    $(function() {
        var links = $("#siderList");
        $(window).scroll(function() {
            var scrollH = $(window).scrollTop();
            var item = $("#list").find(".item");
            item.each(function() {
                var self = $(this),
                    topH = self.offset().top -200;
                var activeId = "";
                if(scrollH >= topH) {
                    activeId = "#" + self.attr("id");
                }else {
                    return false;
                }
                var activeLink = links.find(".active");
                if(activeLink && activeLink.attr("href") != activeId) {
                    activeLink.removeClass("active");
                    links.find("[href="+ activeId +"]").addClass("active");
                }
            })
        })
    })
</script>

 

网页定位导航

标签:

原文地址:http://www.cnblogs.com/wanbi/p/4180586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!