样例 1:基础选项卡 (点击切换)
这是最经典、最基础的选项卡实现,通过点击选项卡标题来切换显示对应的内容区域。

(图片来源网络,侵删)
效果预览
HTML 结构
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">基础选项卡</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="tabs-container">
<!-- 选项卡标题 -->
<div class="tabs">
<button class="tab-button active" data-tab="tab1">选项 1</button>
<button class="tab-button" data-tab="tab2">选项 2</button>
<button class="tab-button" data-tab="tab3">选项 3</button>
</div>
<!-- 选项卡内容 -->
<div class="tab-content">
<div id="tab1" class="tab-pane active">
<h3>选项卡 1 的内容</h3>
<p>这是第一个选项卡的详细内容,你可以在这里放置任何信息。</p>
</div>
<div id="tab2" class="tab-pane">
<h3>选项卡 2 的内容</h3>
<p>这是第二个选项卡的详细内容,点击不同的标题来切换内容。</p>
</div>
<div id="tab3" class="tab-pane">
<h3>选项卡 3 的内容</h3>
<p>这是第三个选项卡的详细内容,恭喜你,已经掌握了基础选项卡的制作!</p>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS 样式 (style.css)
/* 基础样式 */
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f4f4f4;
}
.tabs-container {
width: 80%;
max-width: 600px;
background-color: #fff;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
overflow: hidden; /* 让圆角效果应用到内部元素 */
}
/* 选项卡标题样式 */
.tabs {
display: flex;
background-color: #e0e0e0;
border-bottom: 1px solid #ccc;
}
.tab-button {
flex: 1; /* 让按钮平分宽度 */
padding: 15px 20px;
background: none;
border: none;
cursor: pointer;
font-size: 16px;
font-weight: bold;
color: #555;
transition: background-color 0.3s, color 0.3s;
}
/* 鼠标悬停效果 */
.tab-button:hover {
background-color: #d0d0d0;
}
/* 活动选项卡样式 */
.tab-button.active {
background-color: #fff;
color: #007bff;
border-bottom: 2px solid #007bff;
}
/* 选项卡内容区域样式 */
.tab-content {
padding: 20px;
}
.tab-pane {
display: none; /* 默认隐藏所有内容 */
}
样式 */
.tab-pane.active {
display: block;
}
JavaScript 交互 (script.js)
document.addEventListener('DOMContentLoaded', () => {
// 获取所有选项卡按钮
const tabButtons = document.querySelectorAll('.tab-button');
// 为每个按钮添加点击事件监听器
tabButtons.forEach(button => {
button.addEventListener('click', () => {
// --- 核心逻辑 ---
// 1. 移除所有按钮和内容的 'active' 类
tabButtons.forEach(btn => btn.classList.remove('active'));
document.querySelectorAll('.tab-pane').forEach(pane => pane.classList.remove('active'));
// 2. 为被点击的按钮和对应的内容添加 'active' 类
button.classList.add('active');
// 获取 data-tab 属性的值,找到对应的内容面板
const tabId = button.getAttribute('data-tab');
document.getElementById(tabId).classList.add('active');
});
});
});
样例 2:带动画的选项卡
在基础样例上增加平滑的淡入淡出和滑动效果,提升用户体验。
效果预览切换时有平滑的过渡动画。
HTML 和 CSS (只需修改 style.css)
HTML 结构与样例 1 相同,只需修改 CSS 和 JS。
CSS (style.css) 关键修改:
/* ... 其他样式保持不变 ... */
/* 选项卡内容区域样式 */
.tab-content {
padding: 20px;
position: relative; /* 为绝对定位的子元素提供参考 */
height: 200px; /* 固定高度,以便动画效果更明显 */
overflow: hidden; /* 隐藏超出容器的内容 */
}
.tab-pane {
position: absolute; /* 让所有内容层叠在一起 */
top: 0;
left: 0;
width: 100%;
padding: 20px;
box-sizing: border-box; /* 确保padding不会影响总宽度 */
opacity: 0; /* 默认透明 */
transform: translateX(20px); /* 默认向右偏移 */
transition: opacity 0.4s ease-in-out, transform 0.4s ease-in-out; /* 添加过渡效果 */
}
样式 */
.tab-pane.active {
opacity: 1; /* 显示 */
transform: translateX(0); /* 回到原位 */
}
JavaScript (`script.js)** 与样例 1 完全相同。
样例 3:使用 CSS target 伪类的选项卡 (无需 JS)
这是一个纯 CSS 的实现方法,利用 URL 的 hash 值 () 来切换选项卡,无需任何 JavaScript。
效果预览
通过点击链接改变 URL 的 hash 值(如 #tab1)来切换内容。
HTML 结构 (index.html)
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">CSS :target 选项卡</title>
<style>
/* 基础样式 */
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f4f4f4;
}
.tabs-container {
width: 80%;
max-width: 600px;
background-color: #fff;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
overflow: hidden;
}
.tabs {
display: flex;
background-color: #e0e0e0;
border-bottom: 1px solid #ccc;
}
.tab-button {
flex: 1;
padding: 15px 20px;
background: none;
border: none;
cursor: pointer;
font-size: 16px;
font-weight: bold;
color: #555;
text-decoration: none; /* 移除链接下划线 */
display: block; /* 让 a 标签撑满容器 */
text-align: center;
transition: background-color 0.3s, color 0.3s;
}
.tab-button:hover {
background-color: #d0d0d0;
}
/* 这是核心:当 URL 的 hash 匹配时,链接样式改变 */
.tab-button:target {
background-color: #fff;
color: #007bff;
border-bottom: 2px solid #007bff;
}
.tab-content {
padding: 20px;
}
/* 默认隐藏所有内容面板 */
.tab-pane {
display: none;
}
/* 这是核心:当 URL 的 hash 匹配面板的 id 时,显示该面板 */
.tab-pane:target {
display: block;
}
/* 默认显示第一个面板,通过 URL 的 hash 控制初始状态 */
#tab1:target ~ .tab-content #tab1,
#tab2:target ~ .tab-content #tab2,
#tab3:target ~ .tab-content #tab3 {
display: block;
}
/* 如果没有 hash,默认显示第一个 */
.tab-content > div:first-child {
display: block;
}
</style>
</head>
<body>
<div class="tabs-container">
<!-- 使用锚点链接 -->
<div class="tabs">
<a href="#tab1" class="tab-button">选项 1</a>
<a href="#tab2" class="tab-button">选项 2</a>
<a href="#tab3" class="tab-button">选项 3</a>
</div>
<div class="tab-content">
<div id="tab1" class="tab-pane">
<h3>选项卡 1 的内容 (CSS :target)</h3>
<p>这是第一个选项卡的详细内容,URL 现在是 ...index.html#tab1。</p>
</div>
<div id="tab2" class="tab-pane">
<h3>选项卡 2 的内容 (CSS :target)</h3>
<p>这是第二个选项卡的详细内容,URL 现在是 ...index.html#tab2。</p>
</div>
<div id="tab3" class="tab-pane">
<h3>选项卡 3 的内容 (CSS :target)</h3>
<p>这是第三个选项卡的详细内容,URL 现在是 ...index.html#tab3。</p>
</div>
</div>
</div>
</body>
</html>
这个方法的优缺点:
- 优点: 无需 JavaScript,代码更简洁,对 SEO 友好(每个内容都可以被搜索引擎索引)。
- 缺点: URL 会改变,可能不符合所有场景;样式控制稍微复杂一些(如
target ~ .tab-content #tab2这种选择器)。
总结与选择建议
| 特性 | 样例 1 (JS) | 样例 2 (JS+动画) | 样例 3 (:target) |
|---|---|---|---|
| 实现方式 | JavaScript | JavaScript + CSS | 纯 CSS |
| URL 是否改变 | 否 | 否 | 是 |
| 动画效果 | 无 | 可轻松添加 | 难以实现复杂动画 |
| SEO 友好度 | 一般 | 一般 | 高 |
| 适用场景 | 最通用,大多数项目 | 需要流畅过渡效果的项目 | 单页应用、需要书签功能或SEO优化的场景 |
对于初学者和大多数项目,样例 1 是最好的起点,如果需要更好的用户体验,可以升级到 样例 2,如果你的项目特别注重 SEO 或希望用户可以通过 URL 直接访问特定选项卡,样例 3 是一个非常优雅的纯 CSS 解决方案。
