1. 程式人生 > >網頁定位導航特效的實現

網頁定位導航特效的實現

今天模仿天貓的網頁的導航的特效,五毛特效(好啦,三毛錢,不能再少了)。接下來貼程式碼。

html部分

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>地狗購物網-網頁導航特效</title>
    <link href="css/main.css" rel="stylesheet" type="text/css">
    <!--<script src="js/jquery-2.2.2.min.js" ></script>-->
    <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
    <script src="js/main.js" type="text/javascript"></script>
</head>
<body>
<div id="menu">
    <ul>
        <li><a href="#item1" class="current">1F 男裝</a></li>
        <li><a href="#item2">2F 女裝</a></li>
        <li><a href="#item3">3F 美妝</a></li>
        <li><a href="#item4">4F 數碼</a></li>
        <li><a href="#item5">5F 母嬰</a></li>
    </ul>
</div>
 <div id="content">
     <h1>地狗購物網</h1>
     <div id="item1" class="item">
         <h2>1F 男裝</h2>
         <ul>
             <li><a href="#"><img src="image/1F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/1F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/1F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/1F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/1F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/1F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/1F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/1F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/1F.jpg" alt=""></a></li>
         </ul>
     </div>
     <div id="item2" class="item">
         <h2>2F 女裝</h2>
         <ul>
             <li><a href="#"><img src="image/2F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/2F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/2F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/2F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/2F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/2F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/2F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/2F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/2F.jpg" alt=""></a></li>
         </ul>
     </div>
     <div id="item3" class="item">
         <h2>3F 美妝</h2>
         <ul>
             <li><a href="#"><img src="image/3F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/3F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/3F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/3F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/3F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/3F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/3F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/3F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/3F.jpg" alt=""></a></li>
         </ul>image</div>
     <div id="item4" class="item">
         <h2>4F 數碼</h2>
         <ul>
             <li><a href="#"><img src="image/4F.png" alt=""></a></li>
             <li><a href="#"><img src="image/4F.png" alt=""></a></li>
             <li><a href="#"><img src="image/4F.png" alt=""></a></li>
             <li><a href="#"><img src="image/4F.png" alt=""></a></li>
             <li><a href="#"><img src="image/4F.png" alt=""></a></li>
             <li><a href="#"><img src="image/4F.png" alt=""></a></li>
             <li><a href="#"><img src="image/4F.png" alt=""></a></li>
             <li><a href="#"><img src="image/4F.png" alt=""></a></li>
             <li><a href="#"><img src="image/4F.png" alt=""></a></li>
         </ul> F   </div>
     <div id="item5" class="item">
         <h2>5F 母嬰</h2>
         <ul>
             <li><a href="#"><img src="image/5F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/5F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/5F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/5F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/5F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/5F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/5F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/5F.jpg" alt=""></a></li>
             <li><a href="#"><img src="image/5F.jpg" alt=""></a></li>
         </ul> F F</div>
 </div>

</body>
</html>

css部分

*{
    margin: 0;
    padding:0;
}
body{
    font-size: 12px;
    line-height: 1.7;
}
li{
    list-style: none;
}

#content{
    width: 800px;
    margin:0 auto;
    padding: 20px;
}

#content h1{
    color: #0088bb;
}

#content .item{
    padding: 20px;
    margin-bottom:20px;
    border:1px dotted #0088bb;
}

#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;
    border:none;
}

#menu {
    position: fixed;
    top: 100px;
    left:50%;
    margin-left:400px;
    width:50px;
}

#menu ul li a{
    display: block;
    margin:5px 0;
    font-size:14px;
    font-weight:bold;
    color: #333;
    width:80px;
    height:50px;
    line-height: 50px;
    text-align: center;
    text-decoration: none;
}

#menu ul li a:hover,
#menu ul li a.current{
    color: #fff;
    background: #0088bb;
}

js部分

/**
 * Created by Clam on 2016/7/14.
 */
$(function(){
//    滾動條發生滾動
    //noinspection JSUnresolvedFunction
    $(window).scroll(function(){
        /*在這裡做一些事情*/
        //noinspection JSValidateTypes
        var top;
        //noinspection JSValidateTypes
        top = $(document).scrollTop();
        var menu = $("#menu");
        var items = $("#content").find(".item");

        var currentId = ""; //當前所在的樓層(item) #id

        items.each(function () {
            var m = $(this);
            var itemTop = m.offset().top;
           /* console.log(itemTop);*/
            if(top > itemTop-200){
                currentId = '"#'+ m.attr("id")+'"';
            }
            else {
                return false;
            }
        })
        //給相應的樓層a設定class:crrent,取消其他連結的crrent
        var currentLink = menu.find(".current");
        if(currentId && currentLink.attr("href") != currentId){
            currentLink.removeClass("current");
            menu.find('[href='+ currentId + ']').addClass("current");
        }
    })
});
就這樣咯!