Cursor Trail
光点跟随轨迹
Small points fade behind the moving pointer and briefly show its path.
Where to use it
Add temporary motion feedback to a drawing region or playful feature. Confine a short-lived trail to a surface that invites gestures and leave the normal pointer visible for precise input.
Practical example
In a browser-based handwriting lesson, moving over the practice board leaves a brief trail of dots. A learner can see where the last stroke curved before the dots fade.
AI prompt
Add Cursor Trail to [target]. Reuse a pool of dots to mark pointer positions at a bounded rate. Fade each dot on a short timer and disable trails for reduced motion. Keep the content readable in reduced-motion mode and preserve keyboard navigation.
Example code
Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.
View HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cursor Trail</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.target {
position: relative;
display: grid;
place-items: center;
width: min(100%, 208px);
min-height: 84px;
padding: 10px;
color: #f5efff;
border: 1px solid #aa91ee80;
border-radius: 13px;
background: #29213f;
cursor: pointer;
font: 680 15px system-ui, sans-serif;
}
.target[aria-pressed="true"] { outline: 2px solid #d9c9ff; outline-offset: 3px; }
@media (prefers-reduced-motion: reduce) {
.target, .target *, .target::before, .target::after, .target *::before, .target *::after {
transition: none !important;
animation: none !important;
}
}
.target { min-height: 114px; overflow: hidden; }
.target > span { pointer-events: none; color: #e2d5f8; }
.dot { position: absolute; pointer-events: none; width: 9px; height: 9px; border-radius: 50%; background: #ceacff; box-shadow: 0 0 12px #bca5ff; }
@media (prefers-reduced-motion: reduce) { .dot { display: none; } }
</style>
</head>
<body>
<main class="demo">
<button type="button" class="target" aria-label="Cursor trail"><span>Trace a path</span></button>
</main>
<script>
const target = document.querySelector('.target');
const reduce = matchMedia('(prefers-reduced-motion: reduce)');
const dots = Array.from({ length: 12 }, () => {
const dot = document.createElement('i');
dot.className = 'dot';
dot.hidden = true;
target.append(dot);
return dot;
});
let index = 0;
let lastMove = 0;
function trail(x, y) {
if (reduce.matches) return;
const dot = dots[index++ % dots.length];
dot.getAnimations().forEach((animation) => animation.cancel());
dot.hidden = false;
dot.style.left = x + 'px';
dot.style.top = y + 'px';
dot.animate(
[{ opacity: 1, transform: 'translate(-50%,-50%) scale(1)' }, { opacity: 0, transform: 'translate(-50%,-50%) scale(.2)' }],
{ duration: 390, fill: 'forwards' }
);
}
target.addEventListener('pointermove', (event) => {
const now = performance.now();
if (now - lastMove < 30) return;
lastMove = now;
const bounds = target.getBoundingClientRect();
trail(event.clientX - bounds.left, event.clientY - bounds.top);
});
target.addEventListener('click', (event) => {
const bounds = target.getBoundingClientRect();
trail(event.detail ? event.clientX - bounds.left : bounds.width / 2, event.detail ? event.clientY - bounds.top : bounds.height / 2);
});
</script>
</body>
</html>Original example by UI Skills