jquery - 導航欄滾動監聽

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            body{
                background: #ddd;
            }
            ul{
                list-style: none;
            }
            a{
                text-decoration: none;
            }
            .box{
                margin: 0 auto;
                width: 1200px;
            }
            .fl_l{
                width: 200px;
                float: left;
                border: 1px solid #f4f4f4;
                background: #fff;
                position: fixed;
                top: 0;
                left: 0;
            }
            .fl_l li a{
                border-bottom: 1px solid #eee;
                text-align: center;
                display: block;
                color: #333;
                font-size: 14px;
                line-height: 60px;
            }
            .fl_l li.active a{
                background: #f60;
                color: #fff;
            }
            .fl_r{
                float: right;
                width: 960px;
            }
            .fl_r li{
                margin-bottom: 30px;
                background: #fff;
                font-size: 50px;
                line-height: 300px;
                display: block;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <ul class="fl_l">
                <li class="active"><a href="##">菜單1</a></li>
                <li><a href="##">菜單2</a></li>
                <li><a href="##">菜單3</a></li>
                <li><a href="##">菜單4</a></li>
            </ul>
            <ul class="fl_r">
                <li style="height: 600px;">菜單1內容</li>
                <li style="height: 600px;">菜單2內容</li>
                <li style="height: 400px;">菜單3內容</li>
                <li style="height: 500px;">菜單4內容</li>
            </ul>
            <div style="clear: both;"></div>
        </div>
        <script src="jquery-1.10.1.min.js"></script>
        <script type="text/javascript">
            $(function(){
                $(window).scroll(function(){
                    $('.fl_l li').eq(0).addClass('active');
                    //左側導航加active
                    $('.fl_r li').each(function(){
                        var _target=parseInt($(this).offset().top-$(window).scrollTop());
                        var _i=$(this).index();
                        if (_target<=0) {
                            $('.fl_l li').removeClass('active');
                            $('.fl_l li').eq(_i).addClass('active');
                        }
                        //如果到達頁面底部,給左側導航最後一個加active
                        else if($(document).height()==$(window).scrollTop()+$(window).height()){
                            $('.fl_l li').removeClass('active');
                            $('.fl_l li').eq($('.fl_r li').length-1).addClass('active');
                        }
                    });
                });
                $('.fl_l li').click(function(){
                    $(this).addClass('active').siblings().removeClass('active');
                    var _i=$(this).index();
                     $('body, html').animate({scrollTop:$('.fl_r li').eq(_i).offset().top},500);
                });
            });
        </script>
    </body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章