DIV CSS 佈局教程網

電商左側分類選單實作:jQuery 與原生 JS 兩種寫法
編輯:JQuery入門技巧     

打開各大電商網站,左側那個多層分類選單幾乎是標準配備:滑鼠移入某分類,右側彈出二級選項面板,移出即收起。這個效果看起來複雜,實際上只需要在 hover 時切換一個 class,其餘交給 CSS 排版。以下示範完整做法,並同時附上不用 jQuery 的原生寫法。

先看效果:

一、準備素材

若專案已在用 jQuery,先把它載入即可;不用 jQuery 的可以略過。另外需要幾張圖檔:

icon_nav.jpg(分類小圖示精靈圖):

img_226x40.jpg(標題欄背景):

img_226x95.jpg(選單底部背景):

二、HTML 結構

結構分三層:.product_sort 是整體容器,內部每個 .item 是一個分類,每個 item 又包含標題 .title、簡述 .list、提示箭頭 .arrow、遮線 .line,以及彈出的二級面板 .subitem。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>旅游網站左側分類菜單代碼</title>
<link rel="stylesheet" type="text/css" href="css/index.css" />
</head>
<body>
<div class="product_sort fl">
    <div class="hd">旅游產品導航</div>
    <div class="bd">
        <div class="item">
            <div class="title one"><a href="#"><i></i>出境游</a></div>
            <div class="list">
                <a href="#">泰國</a>
                <a href="#">首爾</a>
                <a href="#">曼谷</a>
                <a href="#">大阪</a>
                <a href="#">普吉島</a>
            </div>
            <div class="arrow">></div>
            <div class="line"></div>
            <div class="subitem">
                <div class="inner">
                    <div class="tit tit1">
                        <div class="name">泰國</div>
                        <ul>
                            <li><a href="#">沙巴</a></li>
                            <li><a href="#">新加坡</a></li>
                            <li><a href="#">菲律賓</a></li>
                            <li><a href="#">吉隆坡</a></li>
                            <li><a href="#">塞班島</a></li>
                            <li><a href="#">吳哥</a></li>
                            <li><a href="#">芽莊</a></li>
                        </ul>
                    </div>
                    <div class="tit">
                        <div class="name">印度尼西亞</div>
                        <ul>
                            <li><a href="#">文萊</a></li>
                            <li><a href="#">藍夢島</a></li>
                            <li><a href="#">柬埔寨</a></li>
                            <li><a href="#">馬來西亞</a></li>
                            <li><a href="#">越南</a></li>
                            <li><a href="#">下龍灣</a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="item">
            <div class="title two"><a href="#"><i></i>國內游</a></div>
            <div class="list">
                <a href="#">泰國</a>
                <a href="#">首爾</a>
                <a href="#">曼谷</a>
            </div>
            <div class="arrow">></div>
            <div class="line"></div>
            <div class="subitem">
                <div class="inner">
                    <div class="tit tit1">
                        <div class="name">泰國</div>
                        <ul>
                            <li><a href="#">沙巴</a></li>
                            <li><a href="#">新加坡</a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
// 方式一:原生 JS
// var a = document.getElementsByClassName("item");
// for (var i = 0; i < a.length; i++) {
//     a[i].onmouseenter = function () {
//         this.className += " layer";
//     };
//     a[i].onmouseleave = function () {
//         this.className = this.className.replace(" layer", "");
//     };
// }

// 方式二:jQuery
$(".product_sort .bd .item").hover(
    function(){ $(this).addClass("layer"); },
    function(){ $(this).removeClass("layer"); }
);
</script>
</body>
</html>

三、CSS 樣式

*{ margin:0; padding:0; list-style: none; text-decoration:none; }

/* 旅游產品分類 */
.product_sort{
    margin:20px; width:226px; height:644px;
    background:#fff url(../images/img_226x95.jpg) no-repeat left bottom;
    border:solid 1px #3299fd; position:relative; z-index:90;
}
.product_sort .hd{
    width:226px; background:url(../images/img_226x40.jpg) no-repeat;
    height:40px; line-height:40px; text-indent:15px;
    font-size:15px; font-weight:bold;
    font-family:"微軟雅黑"; color:#fff;
}
.product_sort .bd{ padding-bottom:10px; }
.product_sort .bd .item{
    border-bottom:solid 1px #3299fd; height:60px; position:relative;
}
.product_sort .bd .item .title{
    width:226px; text-indent:43px; height:30px; overflow:hidden;
    line-height:42px; font-size:14px; font-weight:bold; color:#292929;
}
.product_sort .bd .item .title a{ color:#3299fd; position:relative; }
.product_sort .bd .item .title a i{
    position:absolute; background:url(../images/icon_nav.jpg) no-repeat;
}
.product_sort .bd .item .one a i{
    width:19px; height:19px; background-position:0 0; left:-25px; top:-3px;
}
.product_sort .bd .item .two a i{
    width:22px; height:20px; background-position:0 -51px; left:-28px; top:-5px;
}
.product_sort .bd .item .three a i{
    width:31px; height:23px; background-position:0 -101px; left:-34px; top:-7px;
}
.product_sort .bd .item .list{
    width:210px; padding-left:15px; height:30px; line-height:30px; overflow:hidden;
}
.product_sort .bd .item .list a{ font-family:"微軟雅黑"; color:#333; }
.product_sort .bd .item .list a:hover{ text-decoration:underline; }
.product_sort .bd .item .arrow{
    font-family:"微軟雅黑"; font-size:20px; color:#c0d7f9;
    position:absolute; right:10px; top:14px;
}
.product_sort .bd .item .line{
    position:absolute; right:-2px; top:0px; width:2px; height:60px;
    background-color:#fff; z-index:95; display:none;
}
.product_sort .bd .item .subitem{ display:none; }

/* hover 後的狀態 */
.product_sort .bd .layer .line{ display:block; }
.product_sort .bd .layer .arrow{ display:none; }
.product_sort .bd .layer .subitem{
    width:731px; background:#fff; border:solid 1px #3299fd;
    border-left:none; font-family:"微軟雅黑";
    position:absolute; left:227px; _left:228px; top:-15px; z-index:95;
    min-height:60px; height:auto!important; height:60px;
    padding:5px 10px 15px 10px; display:block;
}
.product_sort .bd .layer .subitem .inner .tit{
    font-size:12px; text-align:left; border-top:1px dashed #d7d7d7; padding:5px 0;
}
.product_sort .bd .layer .subitem .inner .tit1{ border-top:none; }
.product_sort .bd .layer .subitem .inner .tit .name{
    display:block; width:90px; color:#3299fd; cursor:pointer;
    float:left; padding-top:3px;
}
.product_sort .bd .layer .subitem .inner ul{
    overflow:hidden; zoom:1; display:block; margin-left:90px; width:630px;
}
.product_sort .bd .layer .subitem .inner ul li{ float:left; padding:0 8px; height:25px; }
.product_sort .bd .layer .subitem .inner ul li a{ color:#222; display:block; }
.product_sort .bd .layer .subitem .inner ul li a:hover{
    color:#3299fd; text-decoration:underline;
}

四、原理拆解

整個效果的核心只有一句:hover 時加上 layer class,移出時移除。所有視覺變化都寫在 .layer 的 CSS 規則裡——二級面板從 display:none 變成 display:block、箭頭隱藏、右側那條白線出現,讓面板看起來像是與選單連成一體。

幾個值得留意的細節:容器 z-index:90 而面板 z-index:95,確保彈出層壓在其他元素之上。left:227px 讓面板緊貼選單右側,配合 border-left:none 消除接縫。top:-15px 則是為了讓面板首行與選單標題對齊。_left:228px 是給 IE6 的下劃線 hack。

寫成 class 切換而非直接操作 style,有個實際好處:視覺調整全部集中在 CSS,改版面不必碰 JavaScript。這也是目前前端開發普遍推崇的做法。

若行動裝置也需要支援,要留意 hover 在觸控螢幕上沒有對應行為,得另外加上點擊(tap)切換的邏輯,或在小螢幕下直接把二級選項改為展開式顯示。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved