CodeDesign TrendsUncategorized

An Interactive Particle Explosion Effect on User Clicks Using HTML CSS & JavaScript | Techcodetalk.com

Want to create some interactive and fun effect using HTML CSS and JavaScript ? Well at Techcodetalk.com ,today we will learn how to create An Interactive Particle Explosion Effect Using HTML CSS & JavaScript on User Clicks .

Output –

If you know HTML CSS and JavaScript basics , its time to move on an advance level by creating some fun effect.

HTML CODE –

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<link rel=”stylesheet” href=”styles.css”>
<title>Fun Interaction</title>
</head>
<body>
<div class=”animation-container”>
<div class=”particle-container”>
<!– Particles will be generated here –>
</div>
</div>
<script src=”script.js”></script>
</body>
</html>

CSS CODE –

body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #222;
overflow: hidden;
}

.animation-container {
position: relative;
width: 100%;
height: 100%;
}

.particle-container {
position: absolute;
width: 100%;
height: 100%;
pointer-events: none;
display: flex;
justify-content: center;
align-items: center;
}

.particle {
width: 20px;
height: 20px;
background-color: #f06;
border-radius: 50%;
position: absolute;
transform: translate(-50%, -50%);
transition: transform 0.3s, opacity 0.3s;
pointer-events: none;
}

JavaScript CODE –

const particleContainer = document.querySelector(‘.particle-container’);

document.addEventListener(‘click’, (event) => {
const particleCount = 30;

for (let i = 0; i < particleCount; i++) {
const particle = document.createElement(‘div’);
particle.className = ‘particle’;
particle.style.left = `${event.clientX}px`;
particle.style.top = `${event.clientY}px`;

particle.style.backgroundColor = `hsl(${Math.random() * 360}, 100%, 50%)`;
particle.style.transform = `translate(-50%, -50%) scale(${Math.random() * 2})`;

particleContainer.appendChild(particle);

setTimeout(() => {
const angle = Math.random() * Math.PI * 2;
const distance = Math.random() * 200 + 50;
const translateX = Math.cos(angle) * distance;
const translateY = Math.sin(angle) * distance;

particle.style.transform = `translate(${translateX}px, ${translateY}px) scale(0)`;
particle.style.opacity = ‘0’;
}, 10);

setTimeout(() => {
particle.remove();
}, 500);
}
});

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button