全部效果加载与反馈

商品飞入购物车

Fly to Cart

商品缩略图的副本沿弧线飞向购物车,让用户看到商品去了哪里。

点击加入购物车

适用场景

适合加购或收藏后的反馈,用户可以继续浏览列表。 确认添加成功后,再让商品缩略图的副本飞向购物车,并同步更新目标处的件数。

应用示例

陶瓷商城的列表里,加入一只蓝色马克杯后,小杯子的缩略图沿弧线飞向购物车角标。角标数字加一,原商品卡继续留在列表中。

AI 提示词

在【目标商品列表】添加加购成功后的飞入动效。用同一坐标系读取缩略图和购物车图标的位置,让 aria-hidden 副本沿二次曲线飞行并缩小,在完成或取消时清理。每次加购成功只更新一次数量并播报结果。减少动态效果模式下仅更新状态。

示例代码

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

    .shop-demo { position: relative; display: grid; justify-items: end; gap: 12px; width: min(100%, 330px); color: #f1f4dc; }
    .cart { display: flex; gap: 6px; align-items: center; padding: 4px 6px 4px 8px; border: 1px solid #a8be8f8a; border-radius: 7px; background: #243832; font-size: 11px; font-weight: 700; }
    .bag-icon { position: relative; width: 10px; height: 9px; border: 1px solid currentColor; border-radius: 2px; }
    .bag-icon::before { content: ''; position: absolute; left: 2px; top: -5px; width: 4px; height: 5px; border: 1px solid currentColor; border-bottom: none; border-radius: 4px 4px 0 0; }
    .cart-count { min-width: 18px; padding: 2px 4px; border-radius: 4px; background: #b4e19e; color: #1d3428; text-align: center; font-variant-numeric: tabular-nums; }
    .shop-product { display: flex; justify-content: space-between; align-items: center; gap: 14px; width: 100%; padding: 12px; border: 1px solid #a8be8f52; border-radius: 12px; background: linear-gradient(120deg, #30423a, #22313a); }
    .product-actions { display: grid; justify-items: start; gap: 6px; }
    .item-name { font-size: 12px; font-weight: 700; }
    .shop-product .demo-button { color: #203628; background: #c1e9ae; border-color: #e0fadc; font-weight: 650; white-space: nowrap; }
    .shop-product .demo-button:hover { background: #d7f5c9; }
    .camera { position: relative; display: block; width: 59px; height: 40px; flex: none; border-radius: 7px; background: linear-gradient(#f2e8b0 0 27%, #6d8b74 29% 75%, #507364 100%); border: 1px solid #e1e4ad; box-shadow: 0 5px 12px #101c26ad; }
    .camera::before { content: ''; position: absolute; left: 17px; top: 7px; width: 25px; height: 25px; border: 3px solid #c9d7ae; border-radius: 50%; background: radial-gradient(circle at 45% 39%, #bdc9ff 0%, #7b91a8 16%, #33465c 40%, #152c3b 71%); }
    .camera::after { content: ''; position: absolute; right: 4px; top: 4px; width: 7px; height: 4px; border-radius: 1px; background: #324944; }
    .flight-token { position: absolute; z-index: 2; pointer-events: none; }
    .cart-announcement { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
  </style>
</head>
<body>
  <main class="demo">
    <div class="shop-demo">
      <div class="cart" data-cart>
        <span class="bag-icon" aria-hidden="true"></span>
        <span>购物车</span>
        <span class="cart-count" aria-hidden="true" data-testid="effect-demo-cart-count">0</span>
      </div>
      <div class="shop-product">
        <span class="camera" data-product-image aria-hidden="true"></span>
        <div class="product-actions">
          <span class="item-name">便携相机</span>
          <button class="demo-button" type="button" data-testid="effect-demo-add-to-cart">加入购物车</button>
        </div>
      </div>
      <span class="cart-announcement" role="status"></span>
    </div>
  </main>
  <script>
    const shop = document.querySelector('.shop-demo');
    const button = shop.querySelector('button');
    const product = shop.querySelector('[data-product-image]');
    const cart = shop.querySelector('[data-cart]');
    const count = shop.querySelector('.cart-count');
    const announcement = shop.querySelector('[role="status"]');
    const reduceMotion = matchMedia('(prefers-reduced-motion: reduce)');
    let quantity = 0;
    let activeFlights = 0;

    function announceCount(quantity) {
      return '购物车中有 ' + quantity + ' 件商品';
    }

    function flyThumbnail() {
      const origin = product.getBoundingClientRect();
      const destination = cart.getBoundingClientRect();
      const bounds = shop.getBoundingClientRect();
      const dx = destination.left + destination.width / 2 - origin.left - origin.width / 2;
      const dy = destination.top + destination.height / 2 - origin.top - origin.height / 2;
      const token = product.cloneNode(true);
      token.classList.add('flight-token');
      token.style.left = origin.left - bounds.left + 'px';
      token.style.top = origin.top - bounds.top + 'px';
      shop.append(token);
      activeFlights++;

      const keyframes = Array.from({ length: 9 }, (_, index) => {
        const progress = index / 8;
        const x = dx * progress;
        const y = dy * progress - 25 * 4 * progress * (1 - progress);
        return {
          transform: 'translate(' + x + 'px,' + y + 'px) scale(' + (1 - 0.78 * progress) + ')',
          opacity: progress === 1 ? 0 : 1,
        };
      });
      const flight = token.animate(keyframes, { duration: 510, easing: 'cubic-bezier(.22,.7,.36,1)' });
      const cleanUp = () => {
        activeFlights--;
        token.remove();
      };
      flight.finished.then(cleanUp, cleanUp);
    }

    button.addEventListener('click', () => {
      quantity++;
      count.textContent = String(quantity);
      announcement.textContent = announceCount(quantity);
      if (!reduceMotion.matches && typeof product.animate === 'function' && activeFlights < 5) {
        flyThumbnail();
      }
    });
  </script>
</body>
</html>

UI Skills 原创示例

相关参考