Demo
HTML
CSS
JavaScript
<button class="confetti-btn">Click for Confetti!</button>
.confetti-btn {
background: linear-gradient(45deg, #ff6b6b, #ee5a24);
color: white;
border: none;
padding: 1rem 2rem;
font-size: 1.2rem;
font-weight: 600;
border-radius: 50px;
cursor: pointer;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
.confetti-btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
background: linear-gradient(45deg, #ee5a24, #ff6b6b);
}
.confetti-btn:active {
transform: translateY(0);
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}
.confetti-btn::before {
content: '';
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background: linear-gradient(
90deg,
transparent,
rgba(255, 255, 255, 0.2),
transparent
);
transition: left 0.5s ease;
}
.confetti-btn:hover::before {
left: 100%;
}
const confettiBtn = document.querySelector('.confetti-btn');
confettiBtn.addEventListener('click', () => {
const canvas = document.createElement('canvas');
canvas.style.position = 'fixed';
canvas.style.top = '0';
canvas.style.left = '0';
canvas.style.width = '100vw';
canvas.style.height = '100vh';
canvas.style.pointerEvents = 'none';
canvas.style.zIndex = '1000';
document.body.appendChild(canvas);
const confetti = new Confetti(canvas);
confetti.fire({
particleCount: 150,
spread: 70,
origin: { y: 0.6 }
});
setTimeout(() => {
document.body.removeChild(canvas);
}, 3000);
});
Usage Notes
- Dependency: Requires the Confetti.js library to be loaded
- Performance: Large particle counts may affect performance on mobile devices
- Accessibility: Consider adding ARIA labels for screen readers
- Note: To view the source code for this feature, open Chrome DevTools (Inspect) > Sources > CodeVault > HTML(features)/CSS(styles)/JS(scripts) files.