HTML 結構規範
<div class="tab-container">
<ul class="tab-nav" role="tablist">
<li role="tab" aria-selected="true" tabindex="0">選項卡 1</li>
<li role="tab" aria-selected="false" tabindex="-1">選項卡 2</li>
<li role="tab" aria-selected="false" tabindex="-1">選項卡 3</li>
</ul>
<div class="tab-panels">
<div role="tabpanel" class="tab-panel active">內容 1</div>
<div role="tabpanel" class="tab-panel" hidden>內容 2</div>
<div role="tabpanel" class="tab-panel" hidden>內容 3</div>
</div>
</div>
CSS 核心樣式
.tab-nav { display: flex; border-bottom: 2px solid #ddd; margin: 0; padding: 0; list-style: none; }
.tab-nav li { padding: 12px 24px; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -2px; transition: color .2s, border-color .2s; }
.tab-nav li[aria-selected="true"] { color: #007bff; border-color: #007bff; font-weight: 600; }
.tab-panel { display: none; padding: 20px 0; animation: fadeIn .3s ease; }
.tab-panel.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }
jQuery 實現(支持懸停/點擊切換、鍵盤無障礙、動畫)
(function($) {
$.fn.tabSwitch = function(options) {
const opts = $.extend({
event: 'click', // 'click' | 'hover'
activeClass: 'active',
animate: true,
duration: 300,
easing: 'swing',
onSwitch: null // callback(newIndex, oldIndex)
}, options);
return this.each(function() {
const $container = $(this);
const $tabs = $container.find('[role="tab"]');
const $panels = $container.find('[role="tabpanel"]');
let current = $tabs.filter('[aria-selected="true"]').index();
if (current < 0) current = 0;
function activate(index, triggerEvent) {
if (index === current) return;
const old = current;
current = index;
$tabs.attr({ 'aria-selected': 'false', 'tabindex': -1 }).eq(current).attr({ 'aria-selected': 'true', 'tabindex': 0 });
if (opts.animate) {
$panels.stop(true, true).fadeOut(opts.duration).eq(current).fadeIn(opts.duration);
} else {
$panels.hide().eq(current).show();
}
if (opts.onSwitch) opts.onSwitch(current, old, triggerEvent);
}
// 事件綁定
if (opts.event === 'hover') {
$tabs.hover(function() { activate($(this).index(), 'hover'); });
} else {
$tabs.on('click', function() { activate($(this).index(), 'click'); });
}
// 鍵盤支持:左右鍵切換,Home/End 首尾
$tabs.on('keydown', function(e) {
let target = current;
if (e.key === 'ArrowRight') target = (current + 1) % $tabs.length;
else if (e.key === 'ArrowLeft') target = (current - 1 + $tabs.length) % $tabs.length;
else if (e.key === 'Home') target = 0;
else if (e.key === 'End') target = $tabs.length - 1;
else return;
e.preventDefault();
activate(target, 'keyboard');
$tabs.eq(target).trigger('focus');
});
// 初始化激活
activate(current, 'init');
});
};
})(jQuery);
// 使用
$('.tab-container').tabSwitch({ event: 'click', animate: true, duration: 200 });
無障礙 (WAI-ARIA) 完善
role="tablist"、role="tab"、role="tabpanel"。
aria-selected 標識當前激活標籤。
tabindex 管理焦點順序:激活項 0,其餘 -1。
- 面板
hidden 屬性隱藏非激活內容(螢幕閱讀器不讀取)。
aria-controls / id 關聯標籤與面板(可選增強)。
響應式變體:垂直標籤 / 手風琴
@media (max-width: 600px) {
.tab-nav { flex-direction: column; border-bottom: none; border-right: 2px solid #ddd; }
.tab-nav li { border-bottom: none; border-right: 2px solid transparent; margin: 0 0 -2px; }
.tab-nav li[aria-selected="true"] { border-right-color: #007bff; }
}
進階:動態增減標籤
// 新增
function addTab(container, title, content) {
const $c = $(container);
const idx = $c.find('[role="tab"]').length;
$c.find('.tab-nav').append(`${title}`);
$c.find('.tab-panels').append(`${content}
`);
$c.tabSwitch('refresh'); // 需插件暴露 refresh 方法重新綁定
}
// 刪除
function removeTab(container, index) { ... }