跳到正文
网页珍奇柜Web Curio

逐字拆分 CSS 变量

Splitting.js,Stephen Shaw (shshaw) & Christopher Wallis,2018 年

正在布展…

原作直展:这是 Stephen Shaw (shshaw) & Christopher Wallis 的原始作品,按 MIT(附带 Kanit 字体为 OFL) 许可证收录(版本 787de63)。官网是挂在域名根目录的 VuePress 单页应用,无法原样搬进子目录;这里收录 npm 1.1.0 原版库文件,配一张本柜写的演示页:标题逐字弹出、副标题字母波浪、句子逐词滑入,全部靠库写入的 --char-index 等 CSS 变量。点击可重播。 许可证全文 · 收录说明

它是怎么做的

  1. 第 1 步按单词、字符、行、网格单元拆分 DOM。
  2. 第 2 步每个片段获得 --word-index、--char-index 等自定义属性。
  3. 第 3 步父元素获得 --char-total,便于计算相对位置。
  4. 第 4 步动画延迟写成 calc(var(--char-index) * 40ms)。

复刻配方

一段技术配方,可以直接交给 AI 编程助手,也可以自己照着写。想指定技术栈、位置和性能预算,就把它加入实现方案。

Write a small function that walks a text node, wraps each word and each grapheme (use Intl.Segmenter) in spans, and sets style.setProperty('--i', index) on each plus '--total' on the parent. Keep an aria-label with the original string and mark spans aria-hidden. All motion then lives in CSS: transition-delay or animation-delay as calc(var(--i) * 30ms), transforms using sin()/cos() of the index for waves, and a :hover on the parent to replay. Add a line-detection pass by comparing offsetTop to expose a --line index.

档案

能不能拿来用

原作以 MIT 许可开源:可以阅读源码学习,按许可证条款使用;在自己的项目里引用时保留原作者署名。

性能开销

视觉强度

实现难度

尺度为策展判断(1 到 5),用于比较和推荐。

核实记录:Site loads (HTTP 200, title 'Splitting.js'); gh api: MIT, created 2018-01-05.