全部效果自动播放与循环

物体悬浮

Floating Object

物体上下缓缓飘动,地面影子随之变宽、变淡。

播放或暂停演示

适用场景

让产品渲染图或单个插画对象更有空间感。 留出单个物体上下移动的空间,再用地面阴影交代它与底面的距离。

应用示例

磨豆机详情页让刀盘的剖面图在说明旁轻轻悬浮。刀盘向上飘动时,底下的阴影摊开并变淡,看起来像是离开了底座。

AI 提示词

给【目标插画】加物体悬浮效果。用 transform 在 2.8 秒内上下移动不超过 10 像素,最高点让独立阴影变淡。循环提供暂停,减少动态时两个图层都固定。

示例代码

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

    .floating-gem {
      position: absolute; top: 22px; width: 48px; aspect-ratio: 1; border-radius: 12px;
      background: linear-gradient(145deg, #f4f2ac 10%, #91dfc7 54%, #397f9f);
      border: 1px solid #f1f3c57f;
      box-shadow: inset -7px -7px 0 #29918336;
      transform: rotate(28deg); animation: float 2.8s ease-in-out infinite;
    }
    .floating-shadow {
      position: absolute; bottom: 12px; width: 62px; height: 9px; border-radius: 50%;
      background: #030014; filter: blur(4px); animation: soft-shadow 2.8s ease-in-out infinite;
    }
    @keyframes float { 50% { transform: translateY(-10px) rotate(28deg); } }
    @keyframes soft-shadow { 50% { transform: scaleX(1.22); opacity: .43; } }

    .motion-example { display: grid; justify-items: center; width: 100%; gap: 10px; }
    .motion-specimen {
      display: grid;
      place-items: center;
      width: min(100%, 295px);
      height: 108px;
      position: relative;
      isolation: isolate;
    }
    .motion-example [data-motion] { animation-play-state: paused; }
    .motion-example[data-playing] [data-motion] { animation-play-state: running; }
    @media (prefers-reduced-motion: reduce) {
      .motion-example [data-motion] { animation: none; }
      .motion-example [data-loop-toggle] { display: none; }
    }
  </style>
</head>
<body>
  <main class="demo">
    <div class="motion-example">
      <div class="motion-specimen" aria-hidden="true"><div class="floating-gem" data-motion></div><div class="floating-shadow" data-motion></div></div>
      <button class="demo-button" type="button" data-loop-toggle data-testid="effect-demo-loop-toggle">播放演示</button>
    </div>
  </main>
  <script>
    const example = document.querySelector('.motion-example');
    const toggle = example.querySelector('[data-loop-toggle]');

    toggle.addEventListener('click', () => {
      const playing = example.toggleAttribute('data-playing');
      toggle.textContent = playing ? "暂停演示" : "播放演示";
    });
  </script>
</body>
</html>

UI Skills 原创示例