滚动同步章节索引
Chapter Scrollspy
固定索引会高亮当前章节,也能直接跳转到其他章节。
适用场景
帮助读者在指南、文档或案例长文中定位。 给章节设置稳定的锚点,并用固定的阅读位置判断当前章节。高亮可同时改变字重或显示标记,方便分辨。
应用示例
API 参考文档在正文旁保留章节索引。开发者读到「认证」时,对应条目被标为当前章节,点「速率限制」即可跳到那一段。
AI 提示词
给【目标位置】添加滚动同步章节索引。以固定索引下方为阅读线,测量章节锚点。给最近跨过阅读线的章节设置 aria-current,点击跳转时也应用同一偏移。保留键盘操作,并提供减少动态效果时的状态。
示例代码
保存为 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;
}
.scroll-port {
--frame: clamp(112px, 75vh, 242px);
--p: 0;
width: min(100%, 290px); height: var(--frame);
overflow-y:auto; overflow-x:hidden; scroll-behavior:auto;
overscroll-behavior:contain; scrollbar-width:thin;
scrollbar-color:#a79bd5 #20223d; border-radius:12px;
border:1px solid #a79bd567; background:#20233c; color:#f5f2ff;
}
.scroll-port:focus-visible { outline-offset: 3px; }
.scroll-track { position:relative; min-height:calc(var(--frame) + 235px); }
.scroll-pin { position:sticky; top:0; height:var(--frame); overflow:hidden; }
.scroll-pin .tag { position:absolute; z-index:4; left:8px; bottom:9px; font-size:10px; color:#ecfcff; padding:4px 6px; background:#17152ce8; border-radius:4px; }
.scroll-panel { min-height:62px; padding:9px; border:1px solid #adb2e345; border-radius:8px; background:linear-gradient(120deg,#314861,#3e346a); }
.scrollspy { display:flex; gap:4px; position:sticky; top:0; z-index:2; padding:6px; background:#1e2441; border-bottom:1px solid #9c8cef60; }
.scrollspy button { padding:4px 7px; min-width:0; flex:1; font-size:10px; border:1px solid #a99ad25c; border-radius:5px; background:#232c52; color:#e8e5ff; cursor:pointer; }
.scrollspy button[aria-current="location"] { background:#b7a9ed; color:#211c3c; }
.spy-chapter { min-height:calc(var(--frame) - 37px); padding:11px; background:linear-gradient(145deg,hsl(var(--h) 37% 37%),hsl(var(--h) 36% 22%)); }
.spy-chapter i { display:block; height:20px; margin-top:9px; max-width:83px; background:repeating-linear-gradient(0deg,#eff2ff70 0 2px,transparent 2px 6px); border-radius:3px; }
@media (prefers-reduced-motion: reduce) {
.lab, .lab *, .lab::before, .lab::after, .lab *::before, .lab *::after {
animation: none !important;
transition: none !important;
scroll-behavior: auto !important;
}
}
</style>
</head>
<body>
<main class="demo">
<div class="lab scroll-port" role="region" tabindex="0" aria-label="滚动或选择章节"><nav class="scrollspy" aria-label="章节"><button type="button" aria-current="location">港湾</button><button type="button" aria-current="false">灯塔</button><button type="button" aria-current="false">潮线</button></nav>
<section class="spy-chapter" style="--h:163"><strong>港湾</strong><i aria-hidden="true"></i></section><section class="spy-chapter" style="--h:225"><strong>灯塔</strong><i aria-hidden="true"></i></section><section class="spy-chapter" style="--h:271"><strong>潮线</strong><i aria-hidden="true"></i></section></div>
</main>
<script>
const port = document.querySelector('.scroll-port');
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
const listeners = [];
let scheduled = 0;
function sample() {
const max = Math.max(1,port.scrollHeight - port.clientHeight);
return Math.max(0,Math.min(1,port.scrollTop / max));
}
function update() {
scheduled = 0;
const progress = sample();
port.style.setProperty('--p', progress.toFixed(4));
for (const callback of listeners) callback(progress);
}
function schedule() { if (!scheduled) scheduled = requestAnimationFrame(update); }
function onScroll(callback) { listeners.push(callback); callback(sample()); }
port.addEventListener('scroll', schedule, {passive:true});
new ResizeObserver(schedule).observe(port);
reduced.addEventListener('change', schedule);
update();
const nav = port.querySelector('.scrollspy');
const buttons = [...nav.querySelectorAll('button')];
const sections = [...port.querySelectorAll('section')];
function markChapter() {
const line = nav.getBoundingClientRect().bottom + 6;
let active = 0;
sections.forEach((chapter,index) => { if (chapter.getBoundingClientRect().top <= line) active = index; });
if (port.scrollTop > 0 && port.scrollTop + port.clientHeight >= port.scrollHeight - 2) active = sections.length - 1;
buttons.forEach((button,index) => button.setAttribute('aria-current', index === active ? 'location' : 'false'));
}
buttons.forEach((button,index) => button.addEventListener('click',() => {
const top = sections[index].getBoundingClientRect().top - nav.getBoundingClientRect().bottom + port.scrollTop;
port.scrollTo({top,behavior:reduced.matches ? 'instant' : 'smooth'});
}));
onScroll(markChapter);
</script>
</body>
</html>UI Skills 原创示例