全部效果悬停与跟随

卡片倾斜

Card Tilt

指针在卡片上移动时,卡片随指针位置轻微倾斜,移出后回正。

在卡片上移动指针或点按

适用场景

给精选产品、作品封面或作品集卡片增加立体感。 适合单张主推图或间距较宽的作品卡片。控制倾斜角度,让画面主体和旁边的文字保持清楚。

应用示例

陶艺工作室首页可以用这招展示主推花瓶。指针移向卡片边缘时,整张卡片随指针倾斜,移开后慢慢回正。

也叫

  • 3D 倾斜
  • 立体倾斜

AI 提示词

给【目标卡片】加上 Card Tilt。根据指针相对卡片中心的位置改变 X/Y 轴倾斜角度,最大约 8°,移出后平滑回正。沿用项目现有技术栈,保留卡片内容、布局、链接和点击行为。触屏与减少动态效果模式使用静态样式。

示例代码

保存为 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;
    }

    .tilt-area { display: grid; width: min(100%, 216px); perspective: 600px; }
    .tilt {
      --rest-x: 0deg;
      --rest-y: 0deg;
      position: relative;
      display: grid;
      align-content: space-between;
      gap: 21px;
      min-height: 112px;
      padding: 13px 15px;
      overflow: hidden;
      text-align: start;
      color: #fff;
      background: radial-gradient(circle at 98% 1%, #8e70bb, transparent 64%), #281d4c;
      border: 1px solid #beabe978;
      border-radius: 14px;
      box-shadow: 0 10px 22px #0a071c88, inset 0 1px #ffffff28;
      font: inherit;
      cursor: pointer;
      transform: rotateX(var(--tilt-x, var(--rest-x))) rotateY(var(--tilt-y, var(--rest-y)));
      transition: transform 220ms ease-out;
    }
    .tilt:is(:focus-visible, [aria-pressed="true"]) { --rest-x: -8deg; --rest-y: 8deg; }
    .tilt[data-tracking] { transition-duration: 50ms; }
    .tilt::after {
      position: absolute;
      inset: -50% -26% -40% 42%;
      border: 1px solid #e2d0ff55;
      border-radius: 50%;
      transform: rotate(-29deg);
      box-shadow: 0 0 0 10px #dbc3ff15, 0 0 0 21px #dbc3ff12, 0 0 0 33px #dbc3ff0b;
      content: "";
      pointer-events: none;
    }
    .tilt-symbol {
      width: 21px;
      height: 16px;
      background: linear-gradient(135deg, #ffe8cf 10%, #bda3eb);
      border-radius: 4px;
      box-shadow: inset 0 0 0 1px #fff8;
    }
    .tilt-copy { display: grid; gap: 3px; }
    .tilt-copy strong { font-size: 16px; }
    .tilt-copy small { color: #e1d4f4; font-size: 10px; }
    @media (prefers-reduced-motion: reduce) {
      .tilt { transform: none; transition: none; }
      .tilt[aria-pressed="true"] { border-color: #f2dfff; }
    }
  </style>
</head>
<body>
  <main class="demo">
    <div class="tilt-area">
      <button class="tilt" type="button" aria-pressed="false" data-testid="effect-demo-card-tilt">
        <span class="tilt-symbol" aria-hidden="true"></span>
        <span class="tilt-copy"><strong>设计通行证</strong><small>创作工作室</small></span>
      </button>
    </div>
  </main>
  <script>
    const stage = document.querySelector(".tilt-area");
    const card = document.querySelector(".tilt");
    const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)");

    stage.addEventListener("pointermove", (event) => {
      if (event.pointerType === "touch" || reduceMotion.matches) return;
      const { left, top, width, height } = stage.getBoundingClientRect();
      const x = Math.max(-1, Math.min(1, ((event.clientX - left) / width) * 2 - 1));
      const y = Math.max(-1, Math.min(1, ((event.clientY - top) / height) * 2 - 1));
      card.style.setProperty("--tilt-x", (-y * 8) + "deg");
      card.style.setProperty("--tilt-y", (x * 8) + "deg");
      card.setAttribute("data-tracking", "");
    });

    function resetTilt() {
      card.removeAttribute("data-tracking");
      card.style.removeProperty("--tilt-x");
      card.style.removeProperty("--tilt-y");
    }

    stage.addEventListener("pointerleave", resetTilt);
    stage.addEventListener("pointercancel", resetTilt);
    reduceMotion.addEventListener("change", resetTilt);
    card.addEventListener("click", () => {
      const tilted = card.getAttribute("aria-pressed") === "true";
      card.setAttribute("aria-pressed", String(!tilted));
    });
  </script>
</body>
</html>

UI Skills 原创示例

相关参考