Copied!
Guide

How to make a text scramble effect in JavaScript

Letters flicker through random characters and settle into the real word, left to right. It is one small function. Here it is, with the two mistakes that make it jump and annoy screen readers.

← Blog
Title card: Text scramble. Preview of the Letter shuffle menu element from the Enrichr Library.

December 4, 2026

3 min readOne small function

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.

Try it on any site. The free animation library checker shows you which animation libraries actually run on a page (GSAP, Lenis, Framer Motion). Paste a URL, no sign-up for your first lookup.

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:

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?

ElementWhat it does
Letter shuffle menuA menu whose items scramble through random characters and resolve letter by letter.
Character text variantsOne line of text played back by six character-level effects you pick with buttons.
Kinetic type revealsNine ways for a line of type to arrive, including scramble and split-flap.
Scroll text scramble flipText 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

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.