萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JavaScript 實作騰訊遊戲風格二級聯動選單(遊戲分類→遊戲名稱)

模仿騰訊遊戲官網的選單交互:第一層選擇「遊戲分類」,第二層動態載入對應的「遊戲名稱」。本文提供完整的 HTML/CSS/JS 實作,資料結構簡單明確,適合快速套用於遊戲平台、商品分類等雙層級選單場景。

視覺效果預覽

遊戲選單示意圖1

遊戲選單示意圖2

遊戲選單示意圖3

資料結構:二維陣列物件

var List = new Array();
List['紙牌遊戲'] = ['鬥地主', '拖拉機', '橋牌', '拱豬', '打百分'];
List['棋類遊戲'] = ['圍棋', '象棋', '跳棋', '西瓜棋', '五子棋'];
List['其他遊戲'] = ['槍林彈雨', '跑跑卡丁車', '英雄聯盟', 'CF', '英雄三國'];

完整 HTML + JS 實作

<!doctype html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<title>紙牌遊戲選單</title>
<style>
#banner { width: 450px; height: 134px; border: 1px solid #fff; }
.panel { width: 450px; height: 134px; border: 1px solid #fff; background: #1C85B4; padding: 10px; }
.panel p { margin: 10px 0; color: #fff; }
.panel select { width: 200px; padding: 5px; }
</style>
</head>
<body>
<center>
    <div id="banner">
        <img src="images/banner.jpg" width="450" height="134" alt="遊戲橫幅">
    </div>
    <div class="panel">
        <p>請選擇遊戲類別:
            <select id="lei" onchange="ck()">
                <option>--遊戲分類--</option>
            </select>
        </p>
        <p>請選擇遊戲名稱:
            <select id="youxi">
                <option>--遊戲名稱--</option>
            </select>
        </p>
        <p><input type="image" src="images/login.gif" alt="登入"></p>
    </div>
</center>

<script>
var lei = document.getElementById("lei");
var youxi = document.getElementById("youxi");

// 初始化第一層選單
for (var i in List) {
    lei.add(new Option(i, i), null);
}

// 第二層聯動邏輯
lei.onchange = function() {
    var selectedCategory = this.value;
    youxi.options.length = 0; // 清空
    youxi.add(new Option("--遊戲名稱--", ""), null); // 重置預設項
    
    if (List[selectedCategory]) {
        for (var k in List[selectedCategory]) {
            youxi.add(new Option(List[selectedCategory][k], List[selectedCategory][k]), null);
        }
    }
};
</script>
</body>
</html>

關鍵 API 說明

  • select.add(new Option(text, value), null):標準 DOM 方法新增選項,比 createElement 更簡潔
  • select.options.length = 0:一行清空所有選項,效能優於迴圈移除
  • for...in 遍歷物件屬性:適用於鍵值對資料結構

現代化重構:ES6 類別封裝

class GameSelector {
    constructor(categorySel, gameSel, data) {
        this.categoryEl = document.querySelector(categorySel);
        this.gameEl = document.querySelector(gameSel);
        this.data = data;
        this.init();
    }
    
    init() {
        // 填入分類
        Object.keys(this.data).forEach(cat => {
            this.categoryEl.add(new Option(cat, cat));
        });
        
        // 綁定聯動
        this.categoryEl.addEventListener('change', () => this.updateGames());
    }
    
    updateGames() {
        const cat = this.categoryEl.value;
        this.gameEl.innerHTML = '';
        if (this.data[cat]) {
            this.data[cat].forEach(game => {
                this.gameEl.add(new Option(game, game));
            });
        }
    }
}

// 使用
const gameData = {
    '紙牌遊戲': ['鬥地主', '拖拉機', '橋牌', '拱豬', '打百分'],
    '棋類遊戲': ['圍棋', '象棋', '跳棋', '西瓜棋', '五子棋'],
    '其他遊戲': ['槍林彈雨', '跑跑卡丁車', '英雄聯盟', 'CF', '英雄三國']
};

new GameSelector('#lei', '#youxi', gameData);

擴充建議

  • 資料外部化:將 gameData 獨立為 JSON 檔,fetch 載入
  • 加入搜尋/過濾:遊戲多時提供下拉搜尋功能(如 Select2、Choices.js)
  • 無障礙支援:補上 aria-label、鍵盤導航優化
  • 載入狀態:若資料來自 API,顯示 loading spinner
copyright © 萬盛學電腦網 all rights reserved