滚动逐行聚焦带
Scroll Reading Focus Band
阅读时,最接近视窗中心的一行获得背景强调,滚动后焦点随行切换。
适用场景
帮助用户跟读台词、流程短句或逐行注释。 用柔和的背景标出当前行,周围的文字也保持清晰。读者需要回看上一句时可以直接读。
应用示例
访谈视频的逐字稿里,滚动时离面板中线最近的一行会带浅色底。读者看一眼视频再转回字幕,更容易找到刚才读到的那一句。
AI 提示词
给【目标位置】添加滚动逐行聚焦带。将每行中点与视窗中心比较,只强调距离最近的一行。保持所有行的可读对比度与 DOM 阅读顺序。保留键盘操作,并提供减少动态效果时的状态。
示例代码
保存为 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); }
.focus-copy { list-style:none; padding:calc(var(--frame) * .5 - 18px) 10px; margin:0; }
.focus-copy li { min-height:36px; display:grid; place-items:center; border-radius:6px; font-size:13px; color:#c2c0db; }
.focus-copy li[data-center] { color:#feffed; font-weight:700; background:#553e78; box-shadow:inset 0 0 0 1px #c18ae38c; }
.scroll-port:has(.focus-copy) { background:linear-gradient(130deg,#252c4c,#312744); }
@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="滚动让阅读焦点跨行"><ol class="focus-copy"><li>多云转晴。</li><li>低地起雾。</li><li>岛外有风。</li><li>渡船靠岸。</li><li>风停之前。</li><li>城区亮灯。</li><li>码头安静。</li></ol></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 rows = [...port.querySelectorAll('li')];
onScroll(() => {
const center = port.getBoundingClientRect().top + port.clientHeight / 2;
let nearest = null;
let distance = Infinity;
for (const row of rows) {
const bounds = row.getBoundingClientRect();
const gap = Math.abs(bounds.top + bounds.height / 2 - center);
if (gap < distance) { nearest = row; distance = gap; }
}
rows.forEach((row) => row.toggleAttribute('data-center',row === nearest));
});
</script>
</body>
</html>UI Skills 原创示例