北煜ii2026.10 · 读书笔记慢慢读完一本书
Tracing beam
阅读追踪光束
Follow reading progress with a line beside the text.
Also searched as: 文章旁边一道光跟着读到哪里往下走, 左边一条线上有个发光的点跟着阅读移动, 阅读进度做成竖着的光, 读文章时侧边有光跟着滑
- Placement
- Article
- Mood
- Technical, Refined
- Trigger
- Scroll
- Site types
- 博客, 个人网站
How it works & accessibility
Map article scroll progress to the illuminated portion of a vertical SVG path beside the article.
Best for: Article (Scroll).
Reduced motion: 不加光,只留文章旁边那条静止的细线
Mobile & touch: 照常跟着手指滚动;屏幕窄时把线贴近正文(左边留 20px 左右),或者只在宽屏上加
Key parameters
| Parameter | Key | Default | Range | Notes |
|---|---|---|---|---|
| 光有多长(占屏幕高) | length | 30% | — | — |
Ready-to-paste AI prompt
Add “Tracing beam” to the article in my project: Map article scroll progress to the illuminated portion of a vertical SVG path beside the article.
Parameters: length: 30%.
Preserve existing content and layout. Respect prefers-reduced-motion by showing the final state. Content must remain visible if JavaScript fails or is disabled.
Reference: https://donglang.linter.top/effects/tracing-beam.md (canonical specification and reference code).Dependency-free reference code
CSS
/* 阅读追踪光束(Tracing beam)的样式,配合 tracing-beam.js。改写自 Inspira UI 的 TracingBeam(MIT License,Copyright (c) 2024-2026 rahulv.dev):
一条很淡的细线,光是一段渐变(前头亮、往后淡到透明),默认的青色到靛蓝取自它的渐变。
文章的外框加 class="tb":左边留出 --tb-gutter 放线,线离外框左边 --tb-x。线是 ::before 画的,没有脚本、开启"减少动态"时也在。
文章左边没有空地时,线可以放到外面(--tb-gutter 设成 0,--tb-x 设成负数,比如 -24px),但外框和它外面那几层都不能加 overflow: hidden(或 clip),不然线会被裁掉、看不见。
光 .tb-beam 和前头的亮点 .tb-head 由脚本加进来,脚本只改它们的 translate 和 scale;光的高度由脚本按屏幕高算好写上。
颜色:--tb-color 是光的前头和亮点,--tb-tail 是光往后淡掉之前的颜色,--tb-track 是线。
屏幕窄(手机)时线贴近正文:把 --tb-gutter 改小(比如 20px)、--tb-x 改成 6px。 */
.tb {
--tb-color: #18ccfc;
--tb-tail: #6344f5;
--tb-track: rgb(0 0 0 / 0.1);
--tb-gutter: 40px;
--tb-x: 12px;
position: relative;
padding-left: var(--tb-gutter);
}
.tb::before {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: var(--tb-x);
width: 1px;
margin-left: -0.5px;
background: var(--tb-track);
}
.tb-beam,
.tb-head {
position: absolute;
top: 0;
left: var(--tb-x);
pointer-events: none;
}
.tb-beam {
width: 2px;
height: 0;
margin-left: -1px;
border-radius: 1px;
background: linear-gradient(to top, var(--tb-color), var(--tb-tail) 40%, transparent);
transform-origin: top;
will-change: translate, scale;
}
.tb-head {
width: 8px;
height: 8px;
margin: -4px 0 0 -4px;
border-radius: 50%;
background: var(--tb-color);
box-shadow: 0 0 0 3px color-mix(in srgb, var(--tb-color) 22%, transparent), 0 0 12px var(--tb-color);
will-change: translate;
}JavaScript
// 阅读追踪光束(Tracing beam)· 原生 JS,不依赖任何库。
// 改写自 Inspira UI 的 TracingBeam(MIT License,Copyright (c) 2024-2026 rahulv.dev;它移植自 Aceternity UI 的 Tracing Beam):
// 文章左边一条细线,一道光沿着线往下走,前头最亮、后面渐渐淡掉,读到哪里走到哪里。
// 本站改了算法:光的前头按"读到全文的几成"放在线上——文章顶边到屏幕顶边时在线的最上面,文章底边到屏幕底边时在最下面,
// 这样前头在屏幕上也正好从上走到下,一直在正在读的那几行旁边;原版用弹簧追着滚动,这里滚动停下光就停下。前头加了一个亮点。
// articles 是一篇或几篇文章的外框(配合 tracing-beam.css 的 .tb:线画在外框左边留出的地方,是 CSS 画的,没有脚本也在)。
// 脚本往外框里加两个装饰元素(aria-hidden):光 .tb-beam、前头的亮点 .tb-head,只改它们的 translate 和 scale,不引起重新排版。
// length 是光有多长,按屏幕高的百分比(刚开始读时前头离线的顶上还不到这么长,光就短一些)。
// 文章比屏幕高时,滚到页面最底下一定能读完(文章底边总能到屏幕底边);比屏幕矮、又在页面最后的短文章,顶边到不了屏幕顶边,光走不完——
// 这种短文章用不着它。网站顶上有 fixed 的顶栏时,刚开始读的那一小段亮点在顶栏下面看不见:把 view() 返回的上边加上顶栏的高度、高度减去顶栏的高度。
// root 是滚动的容器,默认整个页面(window);文章放在自己会滚动的框里时传那个框。
// 返回 { update(), destroy() }。滚动、改窗口大小、文章自己变高(图片加载完、内容变了)时都会重算。
// 开启"减少动态"时:不加光,只留那条静止的线。ignoreReducedMotion 只给演示用。
/**
* @param {Element | Iterable<Element>} articles
* @param {{ length?: number, root?: Window | HTMLElement, ignoreReducedMotion?: boolean }} [options]
*/
export function tracingBeam(articles, {
length = 30,
root = window,
ignoreReducedMotion = false,
} = {}) {
const reduce = !ignoreReducedMotion && matchMedia('(prefers-reduced-motion: reduce)').matches;
const list = articles instanceof Element ? [articles] : [...articles];
if (reduce || !list.length) return { update() {}, destroy() {} };
const make = (name) => {
const el = document.createElement('span');
el.className = name;
el.setAttribute('aria-hidden', 'true');
return el;
};
const items = list.map((el) => {
const beam = make('tb-beam');
const head = make('tb-head');
el.append(beam, head);
return { el, beam, head, size: -1 };
});
// 屏幕(或滚动框里看得见的那一块)的上边和高度
const view = () => {
if (root === window) return [0, innerHeight];
const r = root.getBoundingClientRect();
return [r.top + root.clientTop, root.clientHeight];
};
const update = () => {
const [top, height] = view();
const size = Math.max(1, (length / 100) * height);
items.forEach((item) => {
const r = item.el.getBoundingClientRect();
// 读到几成:文章比屏幕高时,从"顶边到屏幕顶边"走到"底边到屏幕底边";比屏幕矮时,从"整篇刚露全"走到"顶边到屏幕顶边"
const span = r.height - height;
const raw = span > 0 ? (top - r.top) / span : span < 0 ? (top + height - r.bottom) / -span : Number(r.top <= top);
const at = Math.min(1, Math.max(0, raw)) * r.height;
const from = Math.max(0, at - size);
// 光的长度只在屏幕高变了时才变;变了才写,免得每帧都改样式
if (size !== item.size) {
item.size = size;
item.beam.style.height = `${size}px`;
}
item.beam.style.translate = `0 ${from.toFixed(2)}px`;
item.beam.style.scale = `1 ${((at - from) / size).toFixed(4)}`;
item.head.style.translate = `0 ${at.toFixed(2)}px`;
});
};
let ticking = false;
const schedule = () => {
if (ticking) return;
ticking = true;
requestAnimationFrame(() => {
ticking = false;
update();
});
};
root.addEventListener('scroll', schedule, { passive: true });
window.addEventListener('resize', schedule, { passive: true });
// 文章、滚动框自己变高(图片加载完、内容变了、字体换了)时,没有滚动也没有改窗口大小,也要重算
const resized = new ResizeObserver(schedule);
list.forEach((el) => resized.observe(el));
if (root !== window) resized.observe(root);
update();
return {
update,
destroy() {
root.removeEventListener('scroll', schedule);
window.removeEventListener('resize', schedule);
resized.disconnect();
items.forEach(({ beam, head }) => { beam.remove(); head.remove(); });
},
};
}HTML
<!-- 阅读追踪光束:给文章的外框加 class="tb",线画在它左边留出的地方;光跟着读到哪里走到哪里 -->
<main style="max-width: 680px; margin: 0 auto">
<article class="tb">
<h1>慢慢读完一本书</h1>
<p>第一段……</p>
<img src="插图.jpg" alt="插图" style="width: 100%; height: auto">
<h2>小标题</h2>
<p>第二段……</p>
</article>
</main>
<script type="module">
import { tracingBeam } from './tracing-beam.js';
tracingBeam(document.querySelector('.tb'));
</script>