Fly to Cart
商品飞入购物车
A copy of the product thumbnail arcs into the cart to show where the item was added.
Where to use it
Confirm add-to-cart or save-to-collection actions while shoppers keep browsing. Use a decorative copy of the item and update the number at the destination once the add succeeds.
Practical example
On a ceramics shop’s product grid, adding a blue mug sends a miniature copy of its photo toward the cart badge. The badge increases by one and the original card stays available to browse.
AI prompt
Add a fly-to-cart animation after a successful add action in [target product list]. Measure the thumbnail and cart indicator in the same coordinates. Animate an aria-hidden copy along a quadratic curve, shrink it near the destination, and remove it on completion or cancellation. Give each successful action one count update, announce the result, and skip the flight with reduced motion.
Example code
Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.
View HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fly to Cart</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>Cart</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">Pocket camera</span>
<button class="demo-button" type="button" data-testid="effect-demo-add-to-cart">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 + (quantity === 1 ? ' item in cart' : ' items in cart');
}
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>Original example by UI Skills