中文
山川海报设计视觉识别 · 2026
Hover & pointer · Vanilla JS + CSS

Direction-aware hover

方向感知滑入

Slide an overlay in from the exact edge the pointer entered and out toward the exit edge.

Also searched as: 跟随鼠标进入方向滑入遮罩, 四向感知卡片悬停, 从哪边进从哪边出遮罩, 图片墙方向悬停

AI Markdown (.md)
Placement
Card list, Image
Mood
Crisp, Playful
Trigger
悬停
Site types
品牌官网, 个人网站, 工具和应用

How it works & accessibility

Normalizes cursor coordinates against card dimensions using atan2 to classify top, right, bottom or left entry, animates the overlay from ±100% with WAAPI, reveals cleanly on focus-in, and skips translation under reduced motion.

Best for: Card list, Image (悬停).

Reduced motion: 跳过四向位移动画,鼠标悬停或键盘聚焦时直接显示遮罩层内容

Mobile & touch: 触屏下支持点击卡片切换遮罩显示,或直接把标题常驻显示在底部,不依赖纯悬停

Key parameters

ParameterKeyDefaultRangeNotes
滑入时长duration280ms——
缓动easingcubic-bezier(0.22, 1, 0.36, 1)——

Ready-to-paste AI prompt

Add “Direction-aware hover” to the card list in my project: Normalizes cursor coordinates against card dimensions using atan2 to classify top, right, bottom or left entry, animates the overlay from ±100% with WAAPI, reveals cleanly on focus-in, and skips translation under reduced motion.
Parameters: duration: 280ms, 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/direction-aware-hover.md (canonical specification and reference code).

Dependency-free reference code

CSS
/* 方向感知滑入(Direction-aware hover)样式,配合 direction-aware-hover.js。 */
.daw-card {
  position: relative;
  overflow: hidden;
  border-radius: 12px;
}

.daw-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  align-content: end;
  padding: 14px;
  background: var(--daw-bg, rgb(20 21 25 / 0.84));
  color: var(--daw-ink, #ffffff);
  opacity: 0;
  pointer-events: none;
}

.daw-card[data-active="true"] .daw-overlay {
  opacity: 1;
}

.daw-card.daw-static .daw-overlay {
  opacity: 1;
}
JavaScript
// 方向感知滑入(Direction-aware hover)参考实现。
// 用法:容器 .daw-card 内含遮罩层 .daw-overlay。
// 调 const ctl = directionAwareHover(card, { duration: 280, easing: '...' }); 销毁调 ctl.destroy()。

const OFFSETS = [
  '0 -100%',  // 0: top
  '100% 0',   // 1: right
  '0 100%',   // 2: bottom
  '-100% 0',  // 3: left
];

export function detectDirection(rect, clientX, clientY) {
  const w = rect.width || 1;
  const h = rect.height || 1;
  const x = (clientX - rect.left - w / 2) * (w > h ? h / w : 1);
  const y = (clientY - rect.top - h / 2) * (h > w ? w / h : 1);
  return Math.round(Math.atan2(y, x) / (Math.PI / 2) + 5) % 4;
}

export function directionAwareHover(card, {
  duration = 280,
  easing = 'cubic-bezier(0.22, 1, 0.36, 1)',
  ignoreReducedMotion = false,
} = {}) {
  if (!(card instanceof Element)) return null;
  const overlay = card.querySelector('.daw-overlay');
  if (!overlay) return null;

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

  card.classList.add('daw-card');
  card.classList.remove('daw-static');
  let active = false;
  let anim = null;
  let destroyed = false;

  const stopAnim = () => {
    if (anim) { anim.cancel(); anim = null; }
  };

  const enterFrom = (dir) => {
    if (destroyed) return;
    stopAnim();
    active = true;
    card.dataset.active = 'true';
    card.dataset.dir = String(dir);
    if (isReduced()) {
      overlay.style.opacity = '1';
      overlay.style.translate = '0 0';
      return;
    }
    const offset = OFFSETS[dir] ?? OFFSETS[3];
    anim = overlay.animate(
      [{ translate: offset, opacity: 1 }, { translate: '0 0', opacity: 1 }],
      { duration: dur, easing: easeStr, fill: 'both' },
    );
  };

  const leaveTo = (dir) => {
    if (destroyed) return;
    stopAnim();
    active = false;
    delete card.dataset.active;
    card.dataset.dir = String(dir);
    if (isReduced()) {
      overlay.style.opacity = '';
      overlay.style.translate = '';
      return;
    }
    const offset = OFFSETS[dir] ?? OFFSETS[1];
    anim = overlay.animate(
      [{ translate: '0 0', opacity: 1 }, { translate: offset, opacity: 1 }],
      { duration: dur, easing: easeStr, fill: 'forwards' },
    );
    anim.finished.then(() => {
      if (!destroyed && !active) {
        anim?.cancel();
        anim = null;
        overlay.style.opacity = '';
        overlay.style.translate = '';
      }
    }).catch(() => undefined);
  };

  const onEnter = (ev) => {
    if (ev.pointerType === 'touch') return;
    enterFrom(detectDirection(card.getBoundingClientRect(), ev.clientX, ev.clientY));
  };
  const onLeave = (ev) => {
    if (ev.pointerType === 'touch') return;
    leaveTo(detectDirection(card.getBoundingClientRect(), ev.clientX, ev.clientY));
  };
  const onFocusIn = () => enterFrom(2);
  const onFocusOut = (ev) => { if (!card.contains(ev.relatedTarget)) leaveTo(2); };

  card.addEventListener('pointerenter', onEnter);
  card.addEventListener('pointerleave', onLeave);
  card.addEventListener('focusin', onFocusIn);
  card.addEventListener('focusout', onFocusOut);

  return {
    enterFrom,
    leaveTo,
    isActive: () => active,
    destroy() {
      if (destroyed) return;
      destroyed = true;
      stopAnim();
      card.removeEventListener('pointerenter', onEnter);
      card.removeEventListener('pointerleave', onLeave);
      card.removeEventListener('focusin', onFocusIn);
      card.removeEventListener('focusout', onFocusOut);
      delete card.dataset.active;
      delete card.dataset.dir;
      overlay.style.opacity = '';
      overlay.style.translate = '';
    },
  };
}
HTML
<div class="daw-card" id="card" tabindex="0">
  <img src="/poster.jpg" alt="作品示例" width="240" height="160" />
  <div class="daw-overlay">
    <strong>极简排版作品集</strong>
    <small>网页设计 · 2026</small>
  </div>
</div>

<script type="module">
  import { directionAwareHover } from './direction-aware-hover.js';
  directionAwareHover(document.getElementById('card'));
</script>

More in Hover & pointer

Browse all 86 effects →

Shown once per release · Always available in changelog