HTML 本身并没有原生的 `
html
/ 选项卡容器样式 /
tab-container {
width: 100%;
max-width: 600px;
margin: 20px auto;
/ 选项卡导航按钮样式 /
tab-nav {
display: flex;
border-bottom: 2px solid ddd;
tab-nav button {
padding: 12px 20px;
background: none;
border: none;
cursor: pointer;
font-size: 16px;
transition: all 0.3s;
tab-nav button:hover {
background-color: f0f0f0;
tab-nav button.active {
color: 2196F3;
border-bottom: 3px solid 2196F3;
/ 选项卡内容区域样式 /
tab-content {
padding: 20px;
border: 1px solid ddd;
border-top: none;
tab-pane {
display: none;
tab-pane.active {
display: block;
animation: fadeIn 0.3s;
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
This is the content of tab 1
This is the content of tab 2
This is the content of tab 3
function openTab(evt, tabName) {
// 隐藏所有内容
const tabContents = document.getElementsByClassName("tab-pane");
for (let content of tabContents) {
content.classList.remove("active");
// 移除所有按钮的激活状态
const tabButtons = document.getElementsByTagName("button");
for (let btn of tabButtons) {
btn.classList.remove("active");
// 显示当前选中内容并激活按钮
document.getElementById(tabName).classList.add("active");
evt.currentTarget.classList.add("active");
主要特点:
1. 纯原生实现:不使用任何第三方库
2. 响应式设计:自动适应屏幕宽度
3. 动画效果:切换时带有淡入淡出动画
4. 可定制样式:通过CSS轻松修改颜色、大小等样式
5. 语义化结构:使用合理的HTML标签
自定义建议:
如果需要更复杂的交互(如异步加载内容),可以扩展 `openTab` 函数,加入 AJAX 请求等功能。
版权声明: 知妳网保留所有权利,部分内容为网络收集,如有侵权,请联系QQ793061840删除,添加请注明来意。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态
