全部效果悬停与跟随

程序坞邻近放大

Dock Neighbor Magnification

离指针最近的图标放大最多,相邻图标按距离逐渐减弱。

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

适用场景

帮助用户辨认小型工具栏中正在指向的图标。 这种放大适合图标数量较少的一排入口。给上方留出空间,让图标变大时不推挤相邻内容。

应用示例

网页绘图工具的底部栏放着六个工具。指针靠近钢笔时,它的图标放得最大,相邻的填充和选择图标只放大一点。

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;
    }

    .dock-scene { width:min(100%,280px); height:clamp(106px,68vh,174px); display:grid; place-items:center; align-items:end; padding:12px 0 20px; border:1px solid #a798d45c; border-radius:13px; background: radial-gradient(ellipse at 42% 20%,#394070,#171b34); }
    .dock { width:min(100%,214px); padding:7px; display:flex; justify-content:center; gap:6px; border-radius:15px; background:#c3c9ff34; border:1px solid #eef0ff3d; }
    .dock button { width:30px; height:32px; min-width:0; padding:0; border:0; border-radius:8px; background:none; color:#fff; cursor:pointer; }
    .dock button span { display:grid; place-items:center; width:100%; height:100%; border:1px solid #d1c7f95c; border-radius:inherit; background:#58478a; transform:translateY(calc(var(--weight,0) * -11px)) scale(calc(1 + var(--weight,0) * .44)); transition:transform 130ms ease-out; pointer-events:none; }
    .dock button:nth-child(2n) span { background:#327381; }
    .dock button[aria-pressed="true"] span { background:#af75b6; }

    @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">
    <div class="lab dock-scene"><div class="dock" aria-label="邻近放大程序坞">
    <button type="button" aria-pressed="false" aria-label="预览图标 1"><span aria-hidden="true">◆</span></button><button type="button" aria-pressed="false" aria-label="预览图标 2"><span aria-hidden="true">✦</span></button><button type="button" aria-pressed="false" aria-label="预览图标 3"><span aria-hidden="true">◉</span></button><button type="button" aria-pressed="false" aria-label="预览图标 4"><span aria-hidden="true">△</span></button><button type="button" aria-pressed="false" aria-label="预览图标 5"><span aria-hidden="true">▥</span></button>
    </div></div>
  </main>
  <script>
    const dock = document.querySelector('.dock');
    const buttons = [...dock.querySelectorAll('button')];
    function magnify(x) {
      buttons.forEach((button) => {
        const width = button.offsetWidth;
        const center = button.offsetLeft + width / 2;
        const weight = x === null ? 0 : Math.exp(-Math.pow((x - center) / 38, 2));
        button.style.setProperty('--weight', weight);
      });
    }
    dock.style.position = 'relative';
    function position(button) { return button.offsetLeft + button.offsetWidth / 2; }
    dock.addEventListener('pointermove', (event) => {
      if (event.pointerType !== 'touch' && !matchMedia('(prefers-reduced-motion: reduce)').matches) magnify(event.clientX - dock.getBoundingClientRect().left);
    });
    dock.addEventListener('pointerleave', () => magnify(null));
    dock.addEventListener('focusout', (event) => { if (!dock.contains(event.relatedTarget)) magnify(null); });
    buttons.forEach((button) => {
      button.addEventListener('focus', () => magnify(position(button)));
      button.addEventListener('click', () => {
        const active = button.getAttribute('aria-pressed') !== 'true';
        buttons.forEach((item) => item.setAttribute('aria-pressed', String(item === button && active)));
        magnify(active ? position(button) : null);
      });
    });
  </script>
</body>
</html>

UI Skills 原创示例

相关参考