Copied!
Guide

How to make an image trail that follows the cursor

Pictures appear along the path of the pointer and fade away behind it. The trick is a small pool of elements reused in a loop, and a distance check so they do not pile up. Here is the code.

← Blog
Title card: Cursor image trail. Preview of the Image trail intro element from the Enrichr Library.

December 4, 2026

3 min readA pool of reused elements

An image trail drops pictures along the path of the pointer and fades each one out a moment later. To build it, create a small pool of image elements once, and each time the pointer has moved a set distance, move the next image from the pool to the pointer and play a short fade on it. Plain JavaScript and the Web Animations API are enough.

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?

<div class="trail">
  <img src="a.jpg" alt=""><img src="b.jpg" alt=""><img src="c.jpg" alt="">
  <img src="d.jpg" alt=""><img src="e.jpg" alt=""><img src="f.jpg" alt="">
</div>
.trail { position: relative; height: 80vh; overflow: hidden; }
.trail img {
  position: absolute; left: 0; top: 0;
  width: 180px; aspect-ratio: 4 / 5; object-fit: cover;
  margin: -112px 0 0 -90px;        /* centre the image on the pointer */
  opacity: 0; pointer-events: none; will-change: transform, opacity;
}
const stage = document.querySelector(".trail");
const pool = [...stage.querySelectorAll("img")];
const STEP = 80;                    // pixels between images
let next = 0, z = 1, last = null;

if (!matchMedia("(prefers-reduced-motion: reduce)").matches) {
  stage.addEventListener("pointermove", (e) => {
    const r = stage.getBoundingClientRect();
    const p = { x: e.clientX - r.left, y: e.clientY - r.top };
    if (last && Math.hypot(p.x - last.x, p.y - last.y) < STEP) return;
    last = p;

    const img = pool[next++ % pool.length];
    img.style.zIndex = z++;
    img.animate([
      { opacity: 1, transform: `translate(${p.x}px, ${p.y}px) scale(0.6)` },
      { opacity: 1, transform: `translate(${p.x}px, ${p.y}px) scale(1)`, offset: 0.25 },
      { opacity: 0, transform: `translate(${p.x}px, ${p.y}px) scale(0.9)` }
    ], { duration: 900, easing: "ease-out" });
  });
}

Why a pool?

The pointer can fire a hundred events a second. Making and deleting an element for each one keeps the browser busy building and tearing down the page. With a pool, the page always holds the same six images. The script only moves them and restarts their animation. Calling animate() again on an image that is still fading simply starts it afresh.

Why measure distance and not time?

A timer drops images at the same rate whether the pointer crawls or flies, so slow movement makes a pile and fast movement leaves gaps. A distance check spaces them evenly along the path at any speed.

How do I make it feel better?

Which finished examples can I copy?

ElementWhat it does
Image trail introTiles are emitted along the pointer, or along a looping path when idle.
Repetition image hoverImage tiles that spawn a staggered trail of repeated copies chasing the pointer.
Text trail mousemoveWords spawn along the pointer path, spin as they drift and fade away.
Particles cursorA canvas particle system that emits glowing particles from the pointer.
Ribbon pointer trailA smooth ribbon that chases the pointer, swelling while you move.

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 image trail effect work?

As the pointer moves, the script measures the distance since the last image. Past a threshold, it takes the next image from a small pool, places it at the pointer and plays a short scale-and-fade animation on it.

Why is my image trail slow?

Common causes are creating and removing elements on every move, animating left and top instead of transform, and using full-size photos. Use a pool, transforms and small images.

How many images should the trail show at once?

Between 6 and 12 is plenty. The pool size sets the limit, because the oldest image is reused when the pool runs out.

What happens on touch screens?

There is no hover, so nothing would show. Either run the trail along an automatic path, or follow the finger during a drag.