-
CSS animated order confirmation
HTML
CSS
JavaScript
Add to Cart
Added!
:root { --cart-primary: #4f46e5; /* Indigo */ --cart-hover: #4338ca; --cart-success: #059669; /* Emerald */ --text-color: #ffffff; } body { margin: 25% 30%; } .cart-btn { position: relative; display: flex; align-items: center; justify-content: center; gap: 8px; width: 180px; height: 48px; background-color: var(--cart-primary); color: var(--text-color); border: none; border-radius: 8px; font-size: 16px; font-weight: 600; cursor: pointer; overflow: hidden; transition: background-color 0.3s ease, transform 0.1s ease; } .cart-btn:hover { background-color: var(--cart-hover); } .cart-btn:active { transform: scale(0.96); /* Small click press effect */ } .cart-icon { width: 20px; height: 20px; z-index: 2; transition: transform 0.3s ease; } .btn-text, .btn-success-text { transition: transform 0.4s ease, opacity 0.4s ease; } .btn-success-text { position: absolute; opacity: 0; transform: translateY(20px); /* Starts hidden below */ } /* The box that drops into the cart */ .cart-item { position: absolute; width: 10px; height: 10px; background-color: #fca5a5; /* Light red/pink box */ border-radius: 2px; top: -20px; left: 36px; opacity: 0; z-index: 1; } .cart-btn.is-adding { pointer-events: none; } .cart-btn.is-adding .cart-item { animation: dropItem 0.6s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards; } .cart-btn.is-adding .cart-icon { animation: catchBump 0.3s ease-out 0.4s forwards; } .cart-btn.is-added { background-color: var(--cart-success); pointer-events: none; } .cart-btn.is-added .btn-text { opacity: 0; transform: translateY(-20px); /* Slide text up and out */ } .cart-btn.is-added .btn-success-text { opacity: 1; transform: translateY(0); /* Slide success text in */ } @keyframes dropItem { 0% { top: -20px; opacity: 0; transform: scale(1); } 20% { opacity: 1; } 80% { top: 22px; opacity: 1; transform: scale(1); } 100% { top: 22px; opacity: 0; transform: scale(0); } /* Disappears into cart */ } @keyframes catchBump { 0% { transform: translateY(0); } 50% { transform: translateY(4px) scaleY(0.9); } 100% { transform: translateY(0); } }
document.addEventListener('DOMContentLoaded', () => { const cartBtn = document.getElementById('cart-btn'); cartBtn.addEventListener('click', (e) => { e.preventDefault(); // 1. Start the drop animation cartBtn.classList.add('is-adding'); // 2. Wait for the drop item animation to finish (600ms) setTimeout(() => { cartBtn.classList.remove('is-adding'); cartBtn.classList.add('is-added'); // Optional: Reset button after 2.5 seconds setTimeout(() => { cartBtn.classList.remove('is-added'); }, 2500); }, 600); }); });
Live Preview