DIV CSS 佈局教程網

JS + CSS 實作 Tab 選單切換效果
編輯:關於JavaScript     

Tab 選單是內容分區最常見的介面模式。它的原理其實很單純:用一個陣列存放各分頁的標題與內容,點擊時重繪標籤並更新內容區,同時切換背景色以示目前所在位置。

樣式 index.css

* {
    margin: 0px;
    padding: 0px;
}

body {
    width: 600px;
    margin: 0 auto;
    background-color: silver;
}

#contanier {
    background-color: yellow;
    width: 600px;
    height: 400px;
}

#tabNavi {
    width: 600px;
    height: 30px;
    background-color: #00bfff;
    text-decoration: none;
    list-style-type: none;
}

#tabNavi li {
    float: left;
    margin-right: 7px;
    background-color: #008b8b;
    color: white;
    cursor: pointer;
    width: 60px;
    height: 28px;
    line-height: 30px;
    text-align: center;
}

#content {
    width: 600px;
    height: 370px;
    background-color: white;
}

頁面與腳本 index.html

<!DOCTYPE html> 
<html xmlns="https://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <title>js實現tab菜單動態切換效果</title> 
    <link href="css/index.css" rel="stylesheet" /> 
    <script type="text/javascript"> 
        function gel(id) { 
            return document.getElementById(id); 
        } 

        // 分頁資料:標題與對應內容
        var arr = [ 
            { "title": "新聞", "content": "這是新聞頻道" }, 
            { "title": "財經", "content": "這是財經頻道" }, 
            { "title": "娛樂", "content": "這是娛樂頻道" }, 
            { "title": "體育", "content": "這是體育頻道" }, 
            { "title": "汽車", "content": "這是汽車頻道" }, 
            { "title": "視頻", "content": "這是視頻頻道" }, 
            { "title": "生活", "content": "這是生活頻道" } 
        ]; 

        window.onload = function() { 
            for (var i = 0; i < arr.length; i++) { 
                var liNew = document.createElement("li"); 
                liNew.innerHTML = arr[i].title; 
                gel("tabNavi").appendChild(liNew); 

                // 為每個 li 設定索引屬性,點擊時才能對應到資料
                liNew.setAttribute("id", i); 

                liNew.onclick = function () { 
                    var navs = gel("tabNavi").childNodes; 

                    // 先把所有標籤顏色復原
                    for (var j = 0; j < navs.length; j++) { 
                        if (navs[j].nodeType == 1) { 
                            navs[j].style.backgroundColor = "#008b8b"; 
                        } 
                    } 

                    // 高亮當前標籤
                    this.style.backgroundColor = "red"; 

                    // 更新內容區
                    gel("content").innerHTML = arr[this.id].content; 
                }; 
            } 
        }; 
    </script> 
</head> 
<body> 
    <div id="contanier"> 
        <ul id="tabNavi"></ul> 
        <div id="content" class="content"></div> 
    </div> 
</body> 
</html>

三個實作重點

  • 用 id 當索引:為每個動態產生的 li 寫入索引值,點擊時透過 this.id 就能直接對應到 arr 中的資料,不必反覆搜尋。
  • 先全部復原再高亮:切換時必須先把所有標籤還原成預設色,再設定當前項,否則會出現多個同時高亮的情況。
  • 再次過濾 nodeType:childNodes 含空白文字節點,操作 style 前務必確認是元素節點。

可以再加強的地方

目前的內容寫死在陣列裡,實務上可改為用 hidden 屬性切換多個內容區塊的顯示,或改以 classList 控制狀態樣式,讓 JavaScript 只切 class、視覺交給 CSS。此外加上鍵盤方向鍵操作與 aria-selected 等 ARIA 屬性,能讓 Tab 選單對輔具使用者更友善。

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