全部效果悬停与跟随

方向感应悬停

Direction-Aware Hover

遮罩从指针跨入的边缘滑入,并从指针离开的边缘退场。

移动指针、点按或使用键盘

适用场景

适合带作品详情遮罩的图集卡片。 项目名等必要信息可以常驻,方向感应遮罩用来承载补充信息。

应用示例

设计工作室的案例网格里,指针从右侧进入某张图时,项目简介从右侧滑入。指针从下边缘离开,简介就从下方退出。

AI 提示词

给【目标位置】添加方向感应悬停。在指针进入和离开时比较到四条边的归一化距离,用最近边设置遮罩位移。给键盘与触屏指定默认方向。保留键盘操作,并提供减少动态效果时的状态。

示例代码

保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。

查看 HTML
<!doctype html>
<html lang="zh">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>方向感应悬停</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;
    }

    .pointer-stage {
      --x: 50%; --y: 50%; --dx: 0; --dy: 0;
      display: grid; place-items: center; position: relative;
      width: min(100%, 280px); height: clamp(106px, 68vh, 174px);
      overflow: hidden; isolation: isolate;
      border: 1px solid #b8a8ff70; border-radius: 13px;
      background: #23213e; color: #fcf9ff; cursor: pointer;
      touch-action: manipulation;
    }
    .pointer-stage:focus-visible { outline-offset: 3px; }
    .pointer-stage .tag {
      position: absolute; z-index: 4; bottom: 7px; left: 8px; color: #fff;
      background: #17152fe8; border-radius: 5px; padding: 3px 7px;
      font-size: 10px; line-height: 1.4;
    }

    .direction-orbit { display: grid; place-items: center; width: 48px; aspect-ratio: 1; border: 1px solid #9fe0d5; border-radius: 50%; color: #9fe0d5; font-size: 30px; }
    .direction-cover { position: absolute; inset: 0; display: grid; place-items: center; background: linear-gradient(125deg, #29665f, #214d6b); transform: translate(var(--from-x, 0%), var(--from-y, -103%)); transition: transform 330ms cubic-bezier(.22,.7,.24,1); font-size: 14px; }
    .direction-cover { pointer-events: none; }
    .pointer-stage:is(:hover, :focus-visible, [aria-pressed="true"]) .direction-cover { transform: translate(0,0); }

    @media (prefers-reduced-motion: reduce) {
      .lab, .lab *, .lab::before, .lab::after, .lab *::before, .lab *::after {
        animation: none !important;
        transition: none !important;
        scroll-behavior: auto !important;
      }
    }
  </style>
</head>
<body>
  <main class="demo">
    <button type="button" aria-label="从不同边缘划入" aria-pressed="false" class="lab pointer-stage">
    <span class="direction-orbit" aria-hidden="true">↗</span>
    <span class="direction-cover" aria-hidden="true">从这一边进场</span></button>
  </main>
  <script>
    const stage = document.querySelector('.pointer-stage');
    const reduced = matchMedia('(prefers-reduced-motion: reduce)');
    const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
    function setPosition(x, y) {
      stage.style.setProperty('--x', (x * 100) + '%');
      stage.style.setProperty('--y', (y * 100) + '%');
      stage.style.setProperty('--dx', 2 * x - 1);
      stage.style.setProperty('--dy', 2 * y - 1);
    }
    function position(event) {
      const bounds = stage.getBoundingClientRect();
      return [clamp((event.clientX - bounds.left) / bounds.width, 0, 1), clamp((event.clientY - bounds.top) / bounds.height, 0, 1)];
    }
    stage.addEventListener('pointermove', (event) => {
      if (event.pointerType !== 'touch' && !reduced.matches) setPosition(...position(event));
    });
    stage.addEventListener('pointerleave', () => {
      if (stage.getAttribute('aria-pressed') !== 'true') setPosition(.5, .5);
    });
    stage.addEventListener('click', (event) => {
      const active = stage.getAttribute('aria-pressed') !== 'true';
      stage.setAttribute('aria-pressed', String(active));
      setPosition(...(active && event.detail ? position(event) : active ? [.68, .38] : [.5, .5]));
    });

    function setEdge(event) {
      if (event.pointerType === 'touch' || reduced.matches) return;
      const [x, y] = position(event);
      const edges = [x, 1 - x, y, 1 - y];
      const edge = edges.indexOf(Math.min(...edges));
      stage.style.setProperty('--from-x', ['-103%', '103%', '0%', '0%'][edge]);
      stage.style.setProperty('--from-y', ['0%', '0%', '-103%', '103%'][edge]);
    }
    stage.addEventListener('pointerenter', setEdge);
    stage.addEventListener('pointerleave', setEdge);
  </script>
</body>
</html>

UI Skills 原创示例

相关参考