Here's a fun HTML page with a confetti explosion button using HTML, CSS, and JavaScript:
<!DOCTYPE html>
<html>
<head>
<title>Confetti Button</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.confetti-button {
padding: 20px 40px;
font-size: 24px;
background-color: #ff6b9d;
color: white;
border: none;
border-radius: 50px;
cursor: pointer;
transition: transform 0.2s;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}
.confetti-button:hover {
transform: scale(1.05);
}
.confetti-button:active {
transform: scale(0.95);
}
.confetti {
position: absolute;
width: 10px;
height: 10px;
background-color: #ff6b9d;
pointer-events: none;
}
</style>
</head>
<body>
<button class="confetti-button" onclick="createConfetti()">Click for Confetti! 🎉</button>
<script>
function createConfetti() {
for (let i = 0; i < 50; i++) {
const confetti = document.createElement('div');
confetti.classList.add('confetti');
// Random colors
const colors = ['#ff6b9d', '#4a90e2', '#50e3c2', '#e3d050', '#e350b4'];
confetti.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
// Random starting position
confetti.style.left = Math.random() * window.innerWidth + 'px';
confetti.style.top = window.innerHeight/2 + 'px';
// Random rotation
const rotation = Math.random() * 360;
confetti.style.transform = `rotate(${rotation}deg)`;
document.body.appendChild(confetti);
// Animation
const animation = confetti.animate([
{
transform: `translate(0, 0) rotate(${rotation}deg)`,
opacity: 1
},
{
transform: `translate(${Math.random() * 200 - 100}px, ${-Math.random() * 500}px) rotate(${rotation + 360}deg)`,
opacity: 0
}
], {
duration: 1000 + Math.random() * 1000,
easing: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)'
});
// Remove confetti after animation
animation.onfinish = () => {
confetti.remove();
};
}
}
</script>
</body>
</html>
This code creates a pink button that, when clicked, generates a burst of colorful confetti particles that shoot up and fade away. The confetti pieces:
- Appear from the button's position
- Have random colors from a predefined palette
- Shoot up in random directions
- Rotate as they move
- Fade out and disappear
- Are automatically cleaned up after their animation completes
You can customize the:
- Button appearance by modifying the CSS
- Confetti colors by changing the colors array
- Number of confetti pieces by changing the loop count
- Animation duration and properties
- Confetti size and shape
To use this, just save it as an HTML file and open it in a web browser. Each click will create a new confetti explosion!