滑动标签指示条
Sliding Tab Indicator
切换标签页时,同一条下划线滑到当前标签并调整宽度。
适用场景
在标签页或分段控件中帮助用户跟踪当前选项。 几个标签控制同一块内容、标签文字宽度又不同时,可以让同一条下划线跟随选中项移动和伸缩。
应用示例
发票页用「全部」「待付款」「已结清」切换同一张表。点「待付款」时,下划线从「全部」滑过去并拉长到标签宽度,表内也换成待付款的记录。
AI 提示词
给【目标标签栏】加上 Sliding Tab Indicator。切换选中项时,用约 220 毫秒将同一条下划线移至对应位置并调整宽度,尺寸变化后重新计算。保留标签语义、键盘导航与可见焦点。减少动态效果模式下立即定位下划线。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>滑动标签指示条</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.tabs-demo { display: grid; gap: 8px; width: min(100%, 228px); }
.tab-track {
--tab-index: 0;
position: relative;
isolation: isolate;
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
padding: 3px;
background: #2b223c;
border-radius: 8px;
}
.active-tab-indicator {
position: absolute;
z-index: -1;
inset: 3px auto 3px 3px;
width: calc((100% - 6px) / 3);
background: #d6c4ff;
border-radius: 6px;
transform: translateX(calc(var(--tab-index) * 100%));
transition: transform 260ms cubic-bezier(.2, .75, .2, 1);
}
.period-tab {
padding: 6px 3px;
color: #e3d6f4;
background: transparent;
border: 0;
border-radius: 6px;
font: inherit;
font-size: 10px;
font-weight: 700;
cursor: pointer;
}
.period-tab[aria-selected="true"] { color: #2d1c43; }
.period-panel {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
height: 51px;
padding: 7px 10px;
color: #f4ebff;
background: #332747;
border: 1px solid #624e7a;
border-radius: 8px;
font-size: 17px;
}
.period-panel[hidden] { display: none; }
.bar-group { display: flex; align-items: end; gap: 3px; height: 100%; }
.bar-group i {
width: 5px;
height: var(--bar-height);
background: #bc9deb;
border-radius: 2px 2px 0 0;
}
@media (prefers-reduced-motion: reduce) {
.active-tab-indicator { transition: none; }
}
</style>
</head>
<body>
<main class="demo">
<div class="tabs-demo">
<div class="tab-track" role="tablist" aria-label="访问概况">
<span class="active-tab-indicator" aria-hidden="true"></span>
<button class="period-tab" type="button" role="tab" id="period-0" aria-controls="period-panel-0" aria-selected="true" tabindex="0">当天</button><button class="period-tab" type="button" role="tab" id="period-1" aria-controls="period-panel-1" aria-selected="false" tabindex="-1">本周</button><button class="period-tab" type="button" role="tab" id="period-2" aria-controls="period-panel-2" aria-selected="false" tabindex="-1">本月</button>
</div>
<div class="period-panel" id="period-panel-0" role="tabpanel" aria-labelledby="period-0" tabindex="0">
<strong>124</strong>
<span class="bar-group" aria-hidden="true"><i style="--bar-height: 26%"></i><i style="--bar-height: 74%"></i><i style="--bar-height: 43%"></i><i style="--bar-height: 84%"></i><i style="--bar-height: 56%"></i><i style="--bar-height: 69%"></i></span>
</div>
<div class="period-panel" id="period-panel-1" role="tabpanel" aria-labelledby="period-1" tabindex="0" hidden>
<strong>836</strong>
<span class="bar-group" aria-hidden="true"><i style="--bar-height: 24%"></i><i style="--bar-height: 45%"></i><i style="--bar-height: 39%"></i><i style="--bar-height: 74%"></i><i style="--bar-height: 81%"></i><i style="--bar-height: 64%"></i></span>
</div>
<div class="period-panel" id="period-panel-2" role="tabpanel" aria-labelledby="period-2" tabindex="0" hidden>
<strong>3,140</strong>
<span class="bar-group" aria-hidden="true"><i style="--bar-height: 31%"></i><i style="--bar-height: 43%"></i><i style="--bar-height: 48%"></i><i style="--bar-height: 64%"></i><i style="--bar-height: 79%"></i><i style="--bar-height: 92%"></i></span>
</div>
</div>
</main>
<script>
const track = document.querySelector(".tab-track");
const tabs = Array.from(track.querySelectorAll('[role="tab"]'));
const panels = document.querySelectorAll('[role="tabpanel"]');
function selectPeriod(chosen) {
track.style.setProperty("--tab-index", chosen);
tabs.forEach((tab, index) => {
const active = index === chosen;
tab.setAttribute("aria-selected", String(active));
tab.tabIndex = active ? 0 : -1;
panels[index].hidden = !active;
});
}
tabs.forEach((tab, index) => {
tab.addEventListener("click", () => selectPeriod(index));
tab.addEventListener("keydown", (event) => {
const last = tabs.length - 1;
const positions = {
ArrowRight: index === last ? 0 : index + 1,
ArrowLeft: index === 0 ? last : index - 1,
Home: 0,
End: last,
};
const next = positions[event.key];
if (typeof next !== "number") return;
event.preventDefault();
selectPeriod(next);
tabs[next].focus();
});
});
</script>
</body>
</html>UI Skills 原创示例