萬盛學電腦網

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

jQuery UI Tabs 自動輪播切換實作

jQuery UI Tabs 提供 rotate 方法可實現自動輪播切換分頁。本文完整示範從引入資源到初始化的完整流程。

完整可執行範例

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>jQuery UI Tabs 自動輪播</title>
    <!-- 1. 載入 jQuery 核心 -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- 2. 載入 jQuery UI 核心與 Tabs 模組 -->
    <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script>
    <!-- 3. 載入 jQuery UI 樣式 -->
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
    
    <style>
        #tabs { max-width: 600px; margin: 20px auto; }
        .ui-tabs-panel { padding: 16px; min-height: 100px; }
    </style>
</head>
<body>
    <h1 style="text-align:center">自動輪播分頁示範</h1>
    
    <div id="tabs">
        <ul>
            <li><a href="#tab-news"><span>最新消息</span></a></li>
            <li><a href="#tab-forum"><span>討論區</span></a></li>
            <li><a href="#tab-blog"><span>部落格</span></a></li>
        </ul>
        
        <div id="tab-news">
            <h4>最新消息</h4>
            <p>這裡放置新聞內容...</p>
        </div>
        <div id="tab-forum">
            <h4>討論區</h4>
            <p>論壇精選內容...</p>
        </div>
        <div id="tab-blog">
            <h4>部落格</h4>
            <p>最新文章列表...</p>
        </div>
    </div>

    <script>
        $(function() {
            $('#tabs').tabs({
                // 淡入淡出動畫
                show: { effect: 'fadeIn', duration: 300 },
                hide: { effect: 'fadeOut', duration: 300 },
                // 預設顯示第一個分頁
                active: 0
            }).tabs('rotate', 3000, true); // 3秒輪播,true=持續循環
            
            // 滑鼠懸停暫停輪播
            $('#tabs').hover(
                () => $('#tabs').tabs('rotate', 0, true), // 停止
                () => $('#tabs').tabs('rotate', 3000, true) // 恢復
            );
        });
    </script>
</body>
</html>

關鍵參數說明

// .tabs('rotate', interval, continuing)
$('#tabs').tabs('rotate', 3000, true);
// interval: 毫秒數,0 表示停止輪播
// continuing: true=循環播放,false=播放一次停在最後

進階控制 API

const $tabs = $('#tabs').tabs();

// 程式化切換分頁
$tabs.tabs('option', 'active', 1); // 切換到第 2 個 (0-based)

// 取得目前分頁索引
const currentIndex = $tabs.tabs('option', 'active');

// 動態新增分頁
$tabs.find('.ui-tabs-nav').append('
  • 新分頁
  • '); $tabs.append('
    新內容
    '); $tabs.tabs('refresh'); // 重新整理 Tabs 實例 // 移除分頁 const $panel = $('#tab-forum'); const index = $tabs.find('.ui-tabs-panel').index($panel); $tabs.find('.ui-tabs-nav li').eq(index).remove(); $panel.remove(); $tabs.tabs('refresh');

    現代替代方案:原生 CSS + JS

    不依賴 jQuery UI 的輕量實作:

    <style>
    .tabs { max-width: 600px; margin: 20px auto; font-family: sans-serif; }
    .tab-list { display: flex; border-bottom: 2px solid #3498db; margin: 0; padding: 0; list-style: none; }
    .tab-list button { flex: 1; padding: 12px; background: #f8f9fa; border: none; cursor: pointer; font-size: 1rem; transition: all 0.2s; }
    .tab-list button.active { background: #3498db; color: white; font-weight: bold; }
    .tab-panel { display: none; padding: 20px; border: 1px solid #ddd; border-top: none; }
    .tab-panel.active { display: block; animation: fadeIn 0.3s; }
    @keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
    </style>
    
    <div class="tabs" id="tabs2">
        <ul class="tab-list" role="tablist">
            <li><button role="tab" aria-selected="true" data-tab="news">最新消息</button></li>
            <li><button role="tab" aria-selected="false" data-tab="forum">討論區</button></li>
            <li><button role="tab" aria-selected="false" data-tab="blog">部落格</button></li>
        </ul>
        <div class="tab-panel active" id="news" role="tabpanel"><h4>最新消息</h4><p>內容...</p></div>
        <div class="tab-panel" id="forum" role="tabpanel"><h4>討論區</h4><p>內容...</p></div>
        <div class="tab-panel" id="blog" role="tabpanel"><h4>部落格</h4><p>內容...</p></div>
    </div>
    
    <script>
    const tabs = document.getElementById('tabs2');
    const buttons = tabs.querySelectorAll('[role="tab"]');
    const panels = tabs.querySelectorAll('[role="tabpanel"]');
    let autoRotateTimer;
    let currentIndex = 0;
    
    function showTab(index) {
        buttons.forEach((btn, i) => {
            btn.classList.toggle('active', i === index);
            btn.setAttribute('aria-selected', i === index);
        });
        panels.forEach((panel, i) => panel.classList.toggle('active', i === index));
        currentIndex = index;
    }
    
    function nextTab() {
        showTab((currentIndex + 1) % buttons.length);
    }
    
    function startAutoRotate(interval = 3000) {
        stopAutoRotate();
        autoRotateTimer = setInterval(nextTab, interval);
    }
    
    function stopAutoRotate() {
        clearInterval(autoRotateTimer);
    }
    
    buttons.forEach((btn, i) => {
        btn.addEventListener('click', () => {
            showTab(i);
            startAutoRotate(); // 重置計時器
        });
    });
    
    tabs.addEventListener('mouseenter', stopAutoRotate);
    tabs.addEventListener('mouseleave', startAutoRotate);
    
    startAutoRotate();
    </script>

    無障礙檢查清單

    • role="tablist"、role="tab"、role="tabpanel"
    • aria-selected 標示目前分頁
    • aria-controls 連結 tab 與 panel
    • 鍵盤支援:左/右鍵切換、Home/End 跳首尾
    • 自動輪播提供暫停機制(滑鼠懸停、焦點進入)

    總結

    jQuery UI Tabs 的 rotate 方法提供快速原型開發能力。現代專案建議改用原生實作或輕量庫(如 Tabs from Headless UI、Radix UI),獲得更好的效能、無障礙支援與打包體積控制。

    copyright © 萬盛學電腦網 all rights reserved