アニメーションアンダーラインタブ
アクティブなタブを滑らかに追従するアンダーラインインジケーターを備えたタブナビゲーション。
Tailwind CSSJavaScript
ユーザーがタブを切り替えると、下線が滑らかにスライドして追従する洗練されたタブUIです。
プレビュー (Preview)
Account settings content goes here. Manage your profile details.
ユースケースと特徴 (Use Cases & Features)
- カテゴリ切り替え: 記事のカテゴリ、設定画面のタブなど、同じ画面内で異なるセクションを切り替えるUIに最適です。
- 省スペース化: 限られた画面領域で複数のコンテンツを提供でき、モバイル環境でも有効です。
- 視覚的な現在位置: 下線のアニメーションがタブの現在位置を滑らかに追従することで、ユーザーが今どこを見ているのかを直感的に把握できます。
デザインとUXのポイント (Design & UX)
- アニメーションの追従: CSSの
transitionまたはtransformを用いて、下線がスライドするような動きを実装しています。これにより、タブを切り替えたというフィードバックが心地よく伝わります。 - アクセシビリティへの配慮: アクティブなタブと非アクティブなタブで文字色(コントラスト)を明確に分け、視覚的な区別を容易にしています。
カスタマイズ方法 (Customization Guide)
- カラーの変更:
indigo-600等のクラスを、ブランドカラーに合わせてblue-500やemerald-600に変更するだけでデザインを統一できます。 - タブの追加: HTMLにボタンとコンテンツパネルを追加し、JavaScriptのロジック(
widthやtransformの割合)をタブ数に合わせて調整してください。
実装コード (Implementation)
<div class="max-w-md mx-auto">
<div class="relative flex border-b border-gray-200 dark:border-zinc-800">
<button class="tab-btn flex-1 py-3 text-sm font-medium text-indigo-600 dark:text-indigo-400" data-target="tab1">Account</button>
<button class="tab-btn flex-1 py-3 text-sm font-medium text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200" data-target="tab2">Security</button>
<button class="tab-btn flex-1 py-3 text-sm font-medium text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200" data-target="tab3">Notifications</button>
<div id="tab-indicator" class="absolute bottom-0 left-0 h-0.5 bg-indigo-600 dark:bg-indigo-400 transition-all duration-300 ease-out" style="width: 33.33%; transform: translateX(0%);"></div>
</div>
<div class="pt-4">
<div id="tab1" class="tab-content text-gray-600 dark:text-gray-300 text-sm">Account settings content goes here. Manage your profile details.</div>
<div id="tab2" class="tab-content hidden text-gray-600 dark:text-gray-300 text-sm">Security settings content goes here. Change your password.</div>
<div id="tab3" class="tab-content hidden text-gray-600 dark:text-gray-300 text-sm">Notification preferences go here. Manage email alerts.</div>
</div>
</div>
<script>
const btns = document.querySelectorAll('.tab-btn');
const contents = document.querySelectorAll('.tab-content');
const indicator = document.getElementById('tab-indicator');
btns.forEach((btn, i) => {
btn.addEventListener('click', () => {
btns.forEach(b => {
b.classList.remove('text-indigo-600', 'dark:text-indigo-400');
b.classList.add('text-gray-500', 'dark:text-gray-400');
});
btn.classList.remove('text-gray-500', 'dark:text-gray-400');
btn.classList.add('text-indigo-600', 'dark:text-indigo-400');
indicator.style.transform = `translateX(${i * 100}%)`;
contents.forEach(c => c.classList.add('hidden'));
document.getElementById(btn.dataset.target).classList.remove('hidden');
});
});
</script>