December 4, 2026
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.
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?
- Lag. Place the image at a position that eases toward the pointer, not exactly on it.
- Direction. Rotate each image a few degrees based on which way the pointer was travelling.
- Size. Use images close to the size they are shown at. Six 4000-pixel photos will stutter however good the code is.
Which finished examples can I copy?
| Element | What it does |
|---|---|
| Image trail intro | Tiles are emitted along the pointer, or along a looping path when idle. |
| Repetition image hover | Image tiles that spawn a staggered trail of repeated copies chasing the pointer. |
| Text trail mousemove | Words spawn along the pointer path, spin as they drift and fade away. |
| Particles cursor | A canvas particle system that emits glowing particles from the pointer. |
| Ribbon pointer trail | A 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
- How to make a custom cursor.
- How to make a 3D card tilt hover effect.
- The Enrichr Library: 103 of its 175 elements react to the pointer.
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.