中文

移动鼠标留下卡片轨迹

01 · 海报
02 · 字体
03 · 色彩
04 · 版式
Hover & pointer · Vanilla JS + CSS

Image trail

光标图片拖尾

Spawn fading cards or images along the cursor path as the pointer moves across a stage.

Also searched as: 鼠标移动留下图片轨迹, 光标图片拖尾淡出, 画廊鼠标拖影, 移动指针依次出图

AI Markdown (.md)
Placement
Image, Background
Mood
Playful, Dramatic
Trigger
悬停
Site types
品牌官网, 个人网站

How it works & accessibility

Samples pointer distance against a configurable threshold to avoid node flooding, recycles a bounded pool of items with WAAPI scale-and-fade animations, and falls back to a static layout on touch devices or reduced motion.

Best for: Image, Background (悬停).

Reduced motion: 关闭随指针生成的拖尾动画,保持卡片静态有序展示在舞台内

Mobile & touch: 触屏设备不触发悬停拖尾,直接展示精简后的静态叠放卡片

Key parameters

ParameterKeyDefaultRangeNotes
触发距离threshold48px——
淡出时长duration650ms——
缓动easingcubic-bezier(0.22, 1, 0.36, 1)——

Ready-to-paste AI prompt

Add “Image trail” to the image in my project: Samples pointer distance against a configurable threshold to avoid node flooding, recycles a bounded pool of items with WAAPI scale-and-fade animations, and falls back to a static layout on touch devices or reduced motion.
Parameters: threshold: 48px, duration: 650ms, easing: cubic-bezier(0.22, 1, 0.36, 1).
Preserve existing content and layout. Respect prefers-reduced-motion by showing the final state. Content must remain visible if JavaScript fails or is disabled.
Reference: https://donglang.linter.top/effects/image-trail.md (canonical specification and reference code).

Dependency-free reference code

CSS
/* 光标图片拖尾(Image trail)样式,配合 image-trail.js。 */
.imt-stage {
  position: relative;
  overflow: hidden;
  min-height: 160px;
}

.imt-item {
  position: absolute;
  left: 0;
  top: 0;
  pointer-events: none;
  opacity: 0;
  will-change: transform, opacity, scale;
}

.imt-stage.imt-static .imt-item {
  opacity: 1;
}

@media (prefers-reduced-motion: reduce) {
  .imt-stage:not(.imt-force) .imt-item {
    opacity: 1;
  }
}
JavaScript
// 光标图片拖尾(Image trail)参考实现。
// 用法:容器 .imt-stage 内放若干 .imt-item 元素。
// 调 const ctl = imageTrail(stage, { threshold: 48, duration: 650, easing: '...' }); 销毁调 ctl.destroy()。

export function imageTrail(stage, {
  threshold = 48,
  duration = 650,
  easing = 'cubic-bezier(0.22, 1, 0.36, 1)',
  maxVisible = 6,
  ignoreReducedMotion = false,
} = {}) {
  if (!(stage instanceof Element)) return null;
  const items = [...stage.querySelectorAll('.imt-item')];
  if (!items.length) return null;

  const distMin = Number(threshold) || 48;
  const dur = Number(duration) || 650;
  const easeStr = String(easing);
  const reduceQuery = typeof matchMedia === 'function' ? matchMedia('(prefers-reduced-motion: reduce)') : null;
  const isReduced = () => Boolean(reduceQuery?.matches && !ignoreReducedMotion);

  stage.classList.add('imt-stage');
  stage.classList.remove('imt-static');
  if (ignoreReducedMotion) stage.classList.add('imt-force');

  let index = 0;
  let lastX = null;
  let lastY = null;
  let zTop = 1;
  let destroyed = false;
  const active = new Set();

  const spawn = (localX, localY) => {
    if (destroyed || isReduced()) return null;
    const item = items[index % items.length];
    index += 1;
    // 若同一节点还在播,先取消旧动画复用它
    item.getAnimations().forEach((a) => a.cancel());
    if (active.size >= maxVisible) {
      const oldest = active.values().next().value;
      if (oldest && oldest !== item) {
        oldest.getAnimations().forEach((a) => a.cancel());
        oldest.style.opacity = '0';
        active.delete(oldest);
      }
    }

    const w = item.offsetWidth || 64;
    const h = item.offsetHeight || 48;
    const tilt = ((index % 2 === 0 ? 1 : -1) * ((index % 3) + 2));
    item.style.left = `${Math.round(localX - w / 2)}px`;
    item.style.top = `${Math.round(localY - h / 2)}px`;
    item.style.rotate = `${tilt}deg`;
    item.style.zIndex = String(++zTop);
    active.add(item);

    const anim = item.animate(
      [
        { opacity: 1, scale: '0.75' },
        { opacity: 0, scale: '1' },
      ],
      { duration: dur, easing: easeStr, fill: 'none' },
    );
    anim.finished.then(() => {
      if (!destroyed) active.delete(item);
    }).catch(() => undefined);
    return item;
  };

  const onMove = (ev) => {
    if (destroyed || ev.pointerType === 'touch' || isReduced()) return;
    const rect = stage.getBoundingClientRect();
    const x = ev.clientX - rect.left;
    const y = ev.clientY - rect.top;
    if (lastX === null || lastY === null) {
      lastX = x;
      lastY = y;
      spawn(x, y);
      return;
    }
    if (Math.hypot(x - lastX, y - lastY) >= distMin) {
      lastX = x;
      lastY = y;
      spawn(x, y);
    }
  };

  const onLeave = () => {
    lastX = null;
    lastY = null;
  };

  stage.addEventListener('pointermove', onMove);
  stage.addEventListener('pointerleave', onLeave);

  return {
    spawn,
    activeCount: () => active.size,
    destroy() {
      if (destroyed) return;
      destroyed = true;
      stage.removeEventListener('pointermove', onMove);
      stage.removeEventListener('pointerleave', onLeave);
      stage.classList.remove('imt-force');
      items.forEach((el) => {
        el.getAnimations().forEach((a) => a.cancel());
        el.style.left = '';
        el.style.top = '';
        el.style.rotate = '';
        el.style.zIndex = '';
        el.style.opacity = '';
      });
      active.clear();
    },
  };
}
HTML
<div class="imt-stage" id="trail" style="height: 240px;">
  <div class="imt-item" style="width:96px;height:68px;background:#e0301e;border-radius:8px;"></div>
  <div class="imt-item" style="width:96px;height:68px;background:#141519;border-radius:8px;"></div>
  <div class="imt-item" style="width:96px;height:68px;background:#3f63c4;border-radius:8px;"></div>
  <div class="imt-item" style="width:96px;height:68px;background:#23855a;border-radius:8px;"></div>
</div>

<script type="module">
  import { imageTrail } from './image-trail.js';
  imageTrail(document.getElementById('trail'), { threshold: 48, duration: 650 });
</script>

More in Hover & pointer

Browse all 86 effects →

Shown once per release · Always available in changelog