Glitch text
故障闪烁文字
Briefly split text into offset color layers on hover or trigger while staying within safe flash limits.
Also searched as: 信号错位故障文字, RGB 错位文字闪烁, 赛博故障风文字, 鼠标悬停文字抖动错位
- Placement
- Hero heading, Navigation
- Mood
- Technical, Dramatic
- Trigger
- 悬停
- Site types
- 品牌官网, 个人网站, 工具和应用
How it works & accessibility
Uses two aria-hidden clones clipped horizontally and animated by a configurable pixel shift with WAAPI for a single safe cycle (<= 3Hz); reduced motion leaves the text clear and still.
Best for: Hero heading, Navigation (悬停).
Reduced motion: 关闭红蓝副本层和错位动画,直接保持清晰稳定的原文字
Mobile & touch: 与桌面相同,触发点击或聚焦时短暂错位一次;不依赖一直悬停
Key parameters
| Parameter | Key | Default | Range | Notes |
|---|---|---|---|---|
| 错位时长 | duration | 420ms | — | — |
| 错位幅度 | shift | 5px | — | — |
| 缓动 | easing | cubic-bezier(0.22, 1, 0.36, 1) | — | — |
Ready-to-paste AI prompt
Add “Glitch text” to the hero heading in my project: Uses two aria-hidden clones clipped horizontally and animated by a configurable pixel shift with WAAPI for a single safe cycle (<= 3Hz); reduced motion leaves the text clear and still.
Parameters: duration: 420ms, shift: 5px, easing: cubic-bezier(0.22, 1, 0.36, 1).
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/glitch-text.md (canonical specification and reference code).Dependency-free reference code
CSS
/* 故障闪烁文字(Glitch text)样式,配合 glitch-text.js。 */
.glt-text {
position: relative;
display: inline-block;
}
.glt-layer {
position: absolute;
inset: 0;
pointer-events: none;
opacity: 0;
user-select: none;
}
.glt-layer-a {
color: var(--glt-a, #e0301e);
clip-path: inset(0 0 52% 0);
}
.glt-layer-b {
color: var(--glt-b, #007c83);
clip-path: inset(48% 0 0 0);
}JavaScript
// 故障闪烁文字(Glitch text)参考实现。
// 用法:<span class="glt-text" id="t">CYBER</span>,调 const ctl = glitchText(el, { duration: 420, shift: 5, easing: '...' });
// 悬停/聚焦或调 ctl.trigger() 时播一次低频(WCAG 安全)错位;销毁调 ctl.destroy()。
export function glitchText(el, {
duration = 420,
shift = 5,
easing = 'cubic-bezier(0.22, 1, 0.36, 1)',
ignoreReducedMotion = false,
} = {}) {
if (!(el instanceof Element)) return null;
const text = el.textContent ?? '';
if (!text.trim()) return null;
const dur = Number(duration) || 420;
const dx = Number(shift) || 5;
const easeStr = String(easing);
const reduceQuery = typeof matchMedia === 'function' ? matchMedia('(prefers-reduced-motion: reduce)') : null;
const isReduced = () => Boolean(reduceQuery?.matches && !ignoreReducedMotion);
el.classList.add('glt-text');
const makeLayer = (cls) => {
const s = document.createElement('span');
s.className = `glt-layer ${cls}`;
s.setAttribute('aria-hidden', 'true');
s.textContent = text;
return s;
};
const layerA = makeLayer('glt-layer-a');
const layerB = makeLayer('glt-layer-b');
el.append(layerA, layerB);
let anims = [];
let destroyed = false;
const stop = () => {
anims.forEach((a) => a.cancel());
anims = [];
};
const trigger = () => {
if (destroyed || isReduced()) return;
stop();
const animA = layerA.animate(
[
{ translate: `${-dx}px 0`, opacity: 0.9 },
{ translate: '0 0', opacity: 0 },
],
{ duration: dur, easing: easeStr, fill: 'none' },
);
const animB = layerB.animate(
[
{ translate: `${dx}px 0`, opacity: 0.9 },
{ translate: '0 0', opacity: 0 },
],
{ duration: dur, easing: easeStr, fill: 'none' },
);
anims = [animA, animB];
Promise.all(anims.map((a) => a.finished)).then(() => {
if (!destroyed) anims = [];
}).catch(() => undefined);
};
const onEnter = () => trigger();
const onFocus = () => trigger();
el.addEventListener('pointerenter', onEnter);
el.addEventListener('focusin', onFocus);
return {
trigger,
stop,
destroy() {
if (destroyed) return;
destroyed = true;
stop();
el.removeEventListener('pointerenter', onEnter);
el.removeEventListener('focusin', onFocus);
layerA.remove();
layerB.remove();
},
};
}HTML
<h2 class="glt-text" id="title" tabindex="0">FUTURE MOTION</h2>
<script type="module">
import { glitchText } from './glitch-text.js';
glitchText(document.getElementById('title'), { duration: 420, shift: 5 });
</script>