滚速惯性文字带
Scroll Velocity Marquee
文字带跟随滚动方向和速度移动,输入停止后逐渐减速。
适用场景
给栏目文字带添加短时滚动反馈。 适合重复标题这类辅助信息。用户快速滚过时即使没读完也不影响任务,必要导航放在条带外。
应用示例
爵士音乐节的节目表上方有一条重复的曲风名称。读者快速下滚时,名称向侧面滑过一小段,停止滚动后条带慢慢停住。
AI 提示词
给【目标位置】添加滚速惯性文字带。测量并限制带符号的滚动速度,将它传给带阻尼的水平位移计算。重复文字带,按一组宽度循环位移。保留键盘操作,并提供减少动态效果时的状态。
示例代码
保存为 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); }
.velocity-band { display:flex; align-items:center; position:absolute; inset:24% 0 24%; background:linear-gradient(105deg,#6657a6,#356b8c); overflow:hidden; transform:rotate(-7deg) scale(1.12); }
.velocity-run, .velocity-run span { display:flex; align-items:center; width:max-content; }
.velocity-run { will-change:transform; transform:translateX(var(--offset,0px)); }
.velocity-run b { flex:none; padding:0 16px; font:bold 17px ui-monospace, monospace; color:#e8fff8; }
@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="上下滚动驱动文字带"><div class="scroll-track"><div class="scroll-pin"><div class="velocity-band"><div class="velocity-run"><span><b>天际线</b><b>等高图</b><b>现场记录</b><b>经纬线</b></span><span aria-hidden="true"><b>天际线</b><b>等高图</b><b>现场记录</b><b>经纬线</b></span></div></div><span class="tag" aria-hidden="true">试试不同滚动速度</span></div></div></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 run = port.querySelector('.velocity-run');
let velocity = 0;
let offset = 0;
let lastTop = port.scrollTop;
let lastSample = performance.now();
let lastPaint = lastSample;
let driftFrame = 0;
function drift(now) {
driftFrame = 0;
const elapsed = Math.min(24,now-lastPaint);
lastPaint = now;
const segment = run.firstElementChild.offsetWidth || 1;
offset = ((offset + velocity * elapsed) % segment + segment) % segment;
port.style.setProperty('--offset', -offset + 'px');
velocity *= Math.exp(-elapsed / 135);
if (Math.abs(velocity) > .016) driftFrame = requestAnimationFrame(drift);
}
port.addEventListener('scroll', () => {
const now = performance.now();
if (reduced.matches) { velocity = 0; return; }
const measured = (port.scrollTop - lastTop) / Math.max(16, now-lastSample);
velocity = Math.max(-2.1, Math.min(2.1, measured * 1.25));
lastTop = port.scrollTop;
lastSample = now;
if (!driftFrame) { lastPaint = now; driftFrame = requestAnimationFrame(drift); }
},{passive:true});
reduced.addEventListener('change', () => {
cancelAnimationFrame(driftFrame);
driftFrame = 0;
velocity = 0;
lastTop = port.scrollTop;
lastSample = performance.now();
});
</script>
</body>
</html>UI Skills 原创示例