December 4, 2026
A text scramble effect shows random characters that resolve into the real text, usually from left to right. To build it, redraw the text on every animation frame: real letters before a moving point, random ones after it. One small function does the job, with no library.
What is the code?
const CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#%&";
function scramble(el, duration = 800) {
const final = el.dataset.text || (el.dataset.text = el.textContent);
el.setAttribute("aria-label", final);
if (matchMedia("(prefers-reduced-motion: reduce)").matches) {
el.textContent = final;
return;
}
cancelAnimationFrame(el._raf);
const start = performance.now();
(function tick(now) {
const done = Math.min(1, (now - start) / duration) * final.length;
let out = "";
for (let i = 0; i < final.length; i++) {
out += i < done || final[i] === " "
? final[i]
: CHARS[Math.floor(Math.random() * CHARS.length)];
}
el.textContent = out;
if (done < final.length) el._raf = requestAnimationFrame(tick);
})(start);
}
document.querySelectorAll(".scramble").forEach((el) => {
el.addEventListener("pointerenter", () => scramble(el));
el.addEventListener("focus", () => scramble(el));
});
How do I stop the text from jumping?
An "M" is wider than an "I", so a line of random letters keeps changing length. There are two fixes:
- Use a monospace font for the scrambled text. Every character has the same width.
- Fix the width. Measure the element once with the real text and set that as its
min-width, or wrap each letter in aninline-blockspan that is1chwide.
How do I make it calmer?
Changing every letter 60 times a second looks frantic. Only pick new random characters every third frame, or use a smaller character set such as capitals only. Matching the case of the final text also helps: scramble lowercase words with lowercase letters.
How do I start it when the text scrolls into view?
const io = new IntersectionObserver((entries) => {
for (const e of entries) {
if (e.isIntersecting) { scramble(e.target, 1200); io.unobserve(e.target); }
}
}, { threshold: 0.5 });
document.querySelectorAll(".scramble-in").forEach((el) => io.observe(el));
Which finished examples can I copy?
| Element | What it does |
|---|---|
| Letter shuffle menu | A menu whose items scramble through random characters and resolve letter by letter. |
| Character text variants | One line of text played back by six character-level effects you pick with buttons. |
| Kinetic type reveals | Nine ways for a line of type to arrive, including scramble and split-flap. |
| Scroll text scramble flip | Text lines that slide between two columns as you scroll, then scramble into place. |
All are free in the Enrichr Library, with a live demo, copyable code and a pen on CodePen.
Where to go next
- Split text animation without GSAP.
- How to count numbers up on scroll.
- What to do with animations when a visitor asks for reduced motion.
FAQ
How does the text scramble effect work?
A loop redraws the text many times a second. Characters before a moving index show the final letter, characters after it show a random one. The index advances until the text is complete.
Why does my scrambled text jump around?
Random letters have different widths, so the line gets longer and shorter. Use a monospace font, or put each letter in an inline-block span with a fixed width in ch units.
How do I run the scramble on hover?
Call the function from a pointerenter listener, and add focus too so keyboard users get it. Cancel any run that is still going before starting a new one.
Is a scramble effect accessible?
It can be. Give the element an aria-label with the real text, and when prefers-reduced-motion is set, show the text at once.