Here's a complete HTML file that includes CSS and JavaScript to create an animated weather card for different weather conditions (Wind, Rain, Sun, Snow). The cards are displayed side by side, and you can switch between the different weather conditions using buttons.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Animated Weather Card</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #1a1a1a;
color: white;
font-family: Arial, sans-serif;
}
.weather-container {
display: flex;
gap: 20px;
}
.weather-card {
width: 200px;
height: 300px;
border-radius: 10px;
overflow: hidden;
position: relative;
background-color: #333;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}
.weather-card h2 {
text-align: center;
margin: 10px 0;
}
.animation {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
opacity: 0;
transition: opacity 0.5s;
}
.active {
opacity: 1;
}
/* Wind Animation */
.wind {
background: linear-gradient(to right, #4a4a4a, #6a6a6a);
animation: wind 2s linear infinite;
}
@keyframes wind {
0% { background-position: 0; }
100% { background-position: 100%; }
}
/* Rain Animation */
.rain {
background: linear-gradient(to bottom, #1a1a1a, #2a2a2a);
overflow: hidden;
}
.raindrop {
position: absolute;
width: 2px;
height: 10px;
background: blue;
animation: rain 1s linear infinite;
}
@keyframes rain {
0% { transform: translateY(-100%); }
100% { transform: translateY(100%); }
}
/* Sun Animation */
.sun {
background: radial-gradient(circle, #ffcc00, #ff9900);
animation: shine 2s linear infinite;
}
@keyframes shine {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
/* Snow Animation */
.snow {
background: linear-gradient(to bottom, #1a1a1a, #2a2a2a);
overflow: hidden;
}
.snowflake {
position: absolute;
width: 5px;
height: 5px;
background: white;
border-radius: 50%;
animation: snow 3s linear infinite;
}
@keyframes snow {
0% { transform: translateY(-100%); }
100% { transform: translateY(100%); }
}
button {
margin: 10px;
padding: 10px 20px;
border: none;
border-radius: 5px;
background-color: #555;
color: white;
cursor: pointer;
}
button:hover {
background-color: #777;
}
</style>
</head>
<body>
<div class="weather-container">
<div class="weather-card">
<h2>Wind</h2>
<div class="animation wind active"></div>
</div>
<div class="weather-card">
<h2>Rain</h2>
<div class="animation rain active"></div>
</div>
<div class="weather-card">
<h2>Sun</h2>
<div class="animation sun active"></div>
</div>
<div class="weather-card">
<h2>Snow</h2>
<div class="animation snow active"></div>
</div>
</div>
<div>
<button onclick="showWeather('wind')">Wind</button>
<button onclick="showWeather('rain')">Rain</button>
<button onclick="showWeather('sun')">Sun</button>
<button onclick="showWeather('snow')">Snow</button>
</div>
<script>
function showWeather(condition) {
const animations = document.querySelectorAll('.animation');
animations.forEach(animation => {
animation.classList.remove('active');
});
switch (condition) {
case 'wind':
animations[0].classList.add('active');
break;
case 'rain':
animations[1].classList.add('active');
createRaindrops();
break;
case 'sun':
animations[2].classList.add('active');
break;
case 'snow':
animations[3].classList.add('active');
createSnowflakes();
break;
}
}
function createRaindrops() {
const rainContainer = document.querySelector('.rain');
rainContainer.innerHTML = ''; // Clear previous raindrops
for (let i = 0; i < 20; i++) {
const raindrop = document.createElement('div');
raindrop.className = 'raindrop';
raindrop.style.left = Math.random() * 100 + 'px';
raindrop.style.animationDelay = Math.random() * 2 + 's';
rainContainer.appendChild(raindrop);
}
}
function createSnowflakes() {
const snowContainer = document.querySelector('.snow');
snowContainer.innerHTML = ''; // Clear previous snowflakes
for (let i = 0; i < 20; i++) {
const snowflake = document.createElement('div');
snowflake.className = 'snowflake';
snowflake.style.left = Math.random() * 100 + 'px';
snowflake.style.animationDelay = Math.random() * 3 + 's';
snowContainer.appendChild(snowflake);
}
}
</script>
</body>
</html>
Explanation:
HTML Structure : The HTML consists of a container for the weather cards and buttons to switch between different weather conditions.
CSS Styles : The styles define the appearance of the cards, animations for each weather condition, and button styles.
JavaScript Functionality : The JavaScript includes a function to switch between weather conditions and create raindrops and snowflakes dynamically.
You can copy and paste this code into an HTML file and open it in a web browser to see the animated weather cards in action!