Animating a Shopping Cart
Build an 'add to cart' animation — flying product image, cart badge bounce, and satisfying feedback effects.
Animating a Shopping Cart
Let's create a satisfying "add to cart" animation — when a user clicks the add button, a visual effect plays and the cart badge bounces to indicate the item was added.
The HTML Structure
<header class="shop-header">
<h1>Shop</h1>
<div class="cart-icon" id="cart">
🛒 <span class="cart-badge" id="badge">0</span>
</div>
</header>
<div class="product-grid">
<div class="product-card">
<div class="product-img">📱</div>
<h3>Smartphone</h3>
<p class="price">$699</p>
<button class="add-to-cart-btn" data-name="Smartphone">
Add to Cart
</button>
</div>
<div class="product-card">
<div class="product-img">🎧</div>
<h3>Headphones</h3>
<p class="price">$199</p>
<button class="add-to-cart-btn" data-name="Headphones">
Add to Cart
</button>
</div>
<div class="product-card">
<div class="product-img">⌚</div>
<h3>Smart Watch</h3>
<p class="price">$299</p>
<button class="add-to-cart-btn" data-name="Smart Watch">
Add to Cart
</button>
</div>
</div>
The Keyframe Animations
/* Cart badge bounce when item is added */
@keyframes cartBounce {
0% { transform: scale(1); }
30% { transform: scale(1.5); }
50% { transform: scale(0.9); }
70% { transform: scale(1.2); }
100% { transform: scale(1); }
}
/* Button success feedback */
@keyframes btnSuccess {
0% { transform: scale(1); }
50% { transform: scale(0.95); }
100% { transform: scale(1); }
}
/* Added checkmark animation */
@keyframes checkPop {
0% { transform: scale(0) rotate(-45deg); opacity: 0; }
60% { transform: scale(1.2) rotate(0deg); opacity: 1; }
100% { transform: scale(1) rotate(0deg); opacity: 1; }
}
Styling the Components
.shop-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 40px;
background: #2c3e50;
color: white;
}
.cart-icon {
position: relative;
font-size: 28px;
cursor: pointer;
}
.cart-badge {
position: absolute;
top: -10px;
right: -14px;
background: #e74c3c;
color: white;
font-size: 12px;
font-weight: 700;
min-width: 22px;
height: 22px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
}
.cart-badge.bounce {
animation: cartBounce 0.5s ease;
}
.product-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 24px;
padding: 40px;
max-width: 900px;
margin: 0 auto;
}
.product-card {
background: white;
border-radius: 12px;
padding: 24px;
text-align: center;
box-shadow: 0 4px 16px rgba(0,0,0,0.08);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.product-card:hover {
transform: translateY(-4px);
box-shadow: 0 8px 24px rgba(0,0,0,0.12);
}
.product-img {
font-size: 64px;
margin-bottom: 16px;
}
.product-card h3 {
font-size: 18px;
color: #2c3e50;
margin-bottom: 8px;
}
.price {
font-size: 22px;
font-weight: 700;
color: #27ae60;
margin-bottom: 16px;
}
.add-to-cart-btn {
width: 100%;
padding: 12px;
background: #3498db;
color: white;
border: none;
border-radius: 8px;
font-size: 15px;
font-weight: 600;
cursor: pointer;
transition: background 0.2s ease;
position: relative;
overflow: hidden;
}
.add-to-cart-btn:hover {
background: #2980b9;
}
.add-to-cart-btn.added {
background: #27ae60;
animation: btnSuccess 0.3s ease;
}
The JavaScript
let cartCount = 0;
const badge = document.getElementById('badge');
const buttons = document.querySelectorAll('.add-to-cart-btn');
buttons.forEach(btn => {
btn.addEventListener('click', () => {
// Update count
cartCount++;
badge.textContent = cartCount;
// Button feedback
btn.classList.add('added');
btn.textContent = '✓ Added!';
// Badge bounce
badge.classList.remove('bounce');
void badge.offsetWidth; // Force reflow to restart animation
badge.classList.add('bounce');
// Reset button after 1.5s
setTimeout(() => {
btn.classList.remove('added');
btn.textContent = 'Add to Cart';
}, 1500);
});
});
// Clean up bounce class after animation
badge.addEventListener('animationend', () => {
badge.classList.remove('bounce');
});
The Reflow Trick
void badge.offsetWidth; forces the browser to recalculate the layout, which resets the animation. Without it, removing and re-adding a class in the same frame does nothing.
Animation Timeline
Click "Add to Cart"
├─ Button turns green with "✓ Added!" (btnSuccess 0.3s)
├─ Cart badge number increases
├─ Badge bounces (cartBounce 0.5s)
└─ After 1.5s: button resets to "Add to Cart"
Key Takeaways
- The bounce animation on the badge provides satisfying visual feedback
- Use
void element.offsetWidthto restart CSS animations by forcing a reflow - The
animationendevent lets you clean up classes after an animation completes - Button state changes (color + text) give immediate feedback before resetting
- Combine keyframes (complex effects) with transitions (simple hovers) for best results