Okay, here's a single HTML file that creates four animated weather cards side-by-side, each with a dark background and distinct animations for Sun, Rain, Snow, and Wind.
The JavaScript is used primarily to dynamically create the multiple elements needed for rain and snow effects, making the animations look more natural.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Animated Weather Cards</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #121212; /* Dark background for the page */
display: flex;
justify-content: center;
align-items: flex-start; /* Align cards to the top */
min-height: 100vh;
margin: 0;
padding: 20px;
box-sizing: border-box;
flex-wrap: wrap; /* Allow cards to wrap on smaller screens */
gap: 20px; /* Space between cards */
}
.weather-card {
width: 250px;
height: 350px;
background-color: #2E2E2E; /* Dark background for the card */
border-radius: 15px;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
color: #E0E0E0;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
box-sizing: border-box;
position: relative; /* For positioning animation elements */
overflow: hidden; /* Clip animations within the card */
}
.weather-card h2 {
margin-top: 0;
margin-bottom: 15px;
font-size: 1.5em;
text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
}
.animation-area {
width: 100%;
height: 200px; /* Fixed height for animation consistency */
position: relative;
border-radius: 10px;
/* background: rgba(0,0,0,0.1); Optional: slight tint for animation area */
}
/* --- Sun Animation --- */
.sun-card .animation-area {
background: radial-gradient(circle, #FFEB3B 10%, #FFC107 40%, #FF9800 70%);
}
.sun-rays {
position: absolute;
top: 50%;
left: 50%;
width: 100px;
height: 100px;
transform: translate(-50%, -50%);
}
.sun-rays::before, .sun-rays::after {
content: '';
position: absolute;
background: #FFEB3B;
border-radius: 5px;
animation: sun-radiate 2s infinite linear;
}
.sun-rays::before { /* Horizontal ray */
width: 150px;
height: 10px;
top: 45px; /* (100-10)/2 */
left: -25px; /* (100-150)/2 */
}
.sun-rays::after { /* Vertical ray */
width: 10px;
height: 150px;
top: -25px; /* (100-150)/2 */
left: 45px; /* (100-10)/2 */
}
.sun-rays div { /* Central sun disk */
position: absolute;
top: 50%;
left: 50%;
width: 80px;
height: 80px;
background: #FFEB3B;
border-radius: 50%;
transform: translate(-50%, -50%) scale(1);
animation: sun-pulse 2s infinite ease-in-out;
box-shadow: 0 0 20px #FFEB3B, 0 0 40px #FFC107;
}
@keyframes sun-radiate {
0% { transform: rotate(0deg) scale(1); opacity: 1;}
50% { transform: rotate(10deg) scale(1.1); opacity: 0.8;}
100% { transform: rotate(0deg) scale(1); opacity: 1;}
}
@keyframes sun-pulse {
0% { transform: translate(-50%, -50%) scale(1); box-shadow: 0 0 20px #FFEB3B, 0 0 40px #FFC107; }
50% { transform: translate(-50%, -50%) scale(1.1); box-shadow: 0 0 30px #FFEB3B, 0 0 50px #FFC107; }
100% { transform: translate(-50%, -50%) scale(1); box-shadow: 0 0 20px #FFEB3B, 0 0 40px #FFC107; }
}
/* --- Rain Animation --- */
.rain-card .animation-area {
background: #5A6A7C; /* Cloudy grey */
}
.raindrop {
position: absolute;
bottom: 100%; /* Start above the area */
width: 2px;
height: 15px;
background-color: #A0C0E0; /* Light blue */
border-radius: 50%;
animation: fall linear infinite;
opacity: 0.6;
}
@keyframes fall {
to {
transform: translateY(220px); /* animation-area height + buffer */
opacity: 0;
}
}
/* --- Snow Animation --- */
.snow-card .animation-area {
background: #4A586A; /* Darker cloudy grey */
}
.snowflake {
position: absolute;
top: -10px; /* Start above the area */
font-size: 18px; /* Size of snowflake character */
color: #FFFFFF;
animation: snowfall linear infinite;
opacity: 0.8;
user-select: none; /* Prevent text selection of snowflakes */
}
@keyframes snowfall {
0% {
transform: translateY(0px) translateX(0px) rotate(0deg);
opacity: 1;
}
50% {
transform: translateY(100px) translateX(10px) rotate(180deg);
opacity: 0.9;
}
100% {
transform: translateY(220px) translateX(-10px) rotate(360deg);
opacity: 0;
}
}
/* --- Wind Animation --- */
.wind-card .animation-area {
background: #708090; /* Slate Gray */
}
.cloud {
position: absolute;
background: #B0C4DE; /* LightSteelBlue */
border-radius: 50%;
opacity: 0.7;
animation: move-cloud linear infinite;
}
.cloud.c1 {
width: 80px; height: 50px;
top: 30px; left: -100px; /* Start off-screen */
animation-duration: 10s;
}
.cloud.c2 {
width: 100px; height: 60px;
top: 60px; left: -150px; /* Start off-screen */
animation-duration: 8s;
animation-delay: -3s; /* Stagger animation */
}
.cloud.c3 {
width: 60px; height: 40px;
top: 10px; left: -80px; /* Start off-screen */
animation-duration: 12s;
animation-delay: -6s;
}
/* Pseudo elements for cloud shape */
.cloud::before, .cloud::after {
content: '';
position: absolute;
background: #B0C4DE;
border-radius: 50%;
}
.cloud.c1::before { width: 40px; height: 40px; top: -20px; left: 15px; }
.cloud.c1::after { width: 50px; height: 50px; top: -10px; right: 10px; }
.cloud.c2::before { width: 50px; height: 50px; top: -25px; left: 20px; }
.cloud.c2::after { width: 60px; height: 60px; top: -20px; right: 15px; }
.cloud.c3::before { width: 30px; height: 30px; top: -15px; left: 10px; }
.cloud.c3::after { width: 35px; height: 35px; top: -10px; right: 8px; }
@keyframes move-cloud {
from { transform: translateX(0px); }
to { transform: translateX(400px); } /* Card width + cloud width + buffer */
}
.wind-line {
position: absolute;
height: 1px;
background: #D3D3D3; /* LightGray */
opacity: 0.5;
animation: move-wind-line linear infinite;
}
.wind-line.wl1 { width: 60px; top: 100px; left: -70px; animation-duration: 1.5s; }
.wind-line.wl2 { width: 80px; top: 120px; left: -90px; animation-duration: 1.2s; animation-delay: -0.5s; }
.wind-line.wl3 { width: 50px; top: 140px; left: -60px; animation-duration: 1.8s; animation-delay: -1s; }
@keyframes move-wind-line {
from { transform: translateX(0px); }
to { transform: translateX(350px); } /* Card width + line width + buffer */
}
/* Buttons container (optional, if you want manual switching for a single card later) */
.controls {
display: flex;
justify-content: center;
gap: 10px;
margin-bottom: 20px;
width: 100%;
}
.controls button {
padding: 8px 15px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 0.9em;
}
.controls button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="weather-card sun-card" id="sunCard">
<h2>Sunny</h2>
<div class="animation-area">
<div class="sun-rays">
<div></div> <!-- Central sun disk -->
</div>
</div>
<!-- <p>Bright and cheerful!</p> -->
</div>
<div class="weather-card rain-card" id="rainCard">
<h2>Rainy</h2>
<div class="animation-area" id="rainAnimationArea">
<!-- Raindrops will be generated here by JavaScript -->
</div>
<!-- <p>Don't forget your umbrella!</p> -->
</div>
<div class="weather-card snow-card" id="snowCard">
<h2>Snowy</h2>
<div class="animation-area" id="snowAnimationArea">
<!-- Snowflakes will be generated here by JavaScript -->
</div>
<!-- <p>Winter wonderland!</p> -->
</div>
<div class="weather-card wind-card" id="windCard">
<h2>Windy</h2>
<div class="animation-area">
<div class="cloud c1"></div>
<div class="cloud c2"></div>
<div class="cloud c3"></div>
<div class="wind-line wl1"></div>
<div class="wind-line wl2"></div>
<div class="wind-line wl3"></div>
</div>
<!-- <p>Hold onto your hats!</p> -->
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const rainArea = document.getElementById('rainAnimationArea');
const snowArea = document.getElementById('snowAnimationArea');
const numRaindrops = 50;
const numSnowflakes = 40;
// --- Rain Generation ---
if (rainArea) {
for (let i = 0; i < numRaindrops; i++) {
const raindrop = document.createElement('div');
raindrop.classList.add('raindrop');
raindrop.style.left = Math.random() * 100 + '%';
// Random height/length for raindrops for more variety
raindrop.style.height = (Math.random() * 10 + 10) + 'px';
// Stagger animation start and duration
raindrop.style.animationDuration = (Math.random() * 0.5 + 0.5) + 's'; // 0.5s to 1s
raindrop.style.animationDelay = Math.random() * 2 + 's'; // 0s to 2s
rainArea.appendChild(raindrop);
}
}
// --- Snow Generation ---
if (snowArea) {
const snowflakeChars = ['❄️', '❅', '❆', '•']; // Different snowflake characters
for (let i = 0; i < numSnowflakes; i++) {
const snowflake = document.createElement('div');
snowflake.classList.add('snowflake');
snowflake.innerHTML = snowflakeChars[Math.floor(Math.random() * snowflakeChars.length)];
snowflake.style.left = Math.random() * 100 + '%';
// Vary snowflake size slightly
snowflake.style.fontSize = (Math.random() * 8 + 12) + 'px'; // 12px to 20px
// Stagger animation start and duration for more natural fall
snowflake.style.animationDuration = (Math.random() * 3 + 4) + 's'; // 4s to 7s
snowflake.style.animationDelay = Math.random() * 5 + 's'; // 0s to 5s
// Slight horizontal variation in animation itself (CSS handles main path)
// The CSS @keyframes snowfall already includes some horizontal drift
snowArea.appendChild(snowflake);
}
}
// Note: The prompt asked for a way to switch conditions.
// Since all cards are shown side-by-side, they are already demonstrating their respective animations.
// If you wanted a SINGLE card that changes, you would:
// 1. Have one <div class="weather-card" id="dynamicCard">
// 2. Have buttons: <button onclick="setWeather('sun')">Sun</button>, etc.
// 3. The setWeather(type) function would:
// a. Get `document.getElementById('dynamicCard')` and its animation area.
// b. Clear previous animation elements (`animationArea.innerHTML = ''`).
// c. Remove old classes (e.g., `card.classList.remove('sun-card', 'rain-card')`).
// d. Add new class (e.g., `card.classList.add(type + '-card')`).
// e. Update `<h2>` text.
// f. Call specific functions to populate animation (e.g., if type === 'rain', call a createRain function).
// This setup fulfills the "show all side by side" requirement directly.
});
</script>
</body>
</html>
Explanation:
-
HTML Structure:
- A
body with display: flex to arrange cards side-by-side.
- Four
div elements with class weather-card, each also having a specific class like sun-card, rain-card, etc., to target unique styles and animations.
- Each card has an
<h2> for the title and a div with class animation-area where the visual effects occur.
- The
rainAnimationArea and snowAnimationArea have IDs so JavaScript can target them to add particles.
- The Sun and Wind animations are mostly achieved with static HTML elements styled and animated by CSS.
-
CSS Styling:
body: Basic dark theme, flexbox for layout.
.weather-card: Defines the appearance of each card (dark background, rounded corners, shadow, dimensions, overflow: hidden to clip animations). position: relative is crucial for absolutely positioning animation elements within the card.
.animation-area: A container within the card for the animations.
- Sun Animation (
.sun-card):
- Uses a
radial-gradient for the bright background.
.sun-rays and its pseudo-elements (::before, ::after) create static rays.
- The central
div inside .sun-rays is the sun disk, animated with sun-pulse (scaling and shadow) and sun-radiate (slight rotation on rays).
- Rain Animation (
.rain-card):
.raindrop: Styled as a thin, tall element. The fall keyframes animation moves it downwards and fades it out. JavaScript will create many of these.
- Snow Animation (
.snow-card):
.snowflake: Styled for snowflake characters. The snowfall keyframes animation gives it a falling and slight swaying motion. JavaScript will create many of these.
- Wind Animation (
.wind-card):
.cloud: Basic divs styled with and pseudo-elements to create a cloud-like shape. The animation translates them horizontally across the card. Multiple clouds with different animation durations/delays create a parallax effect.
To Use:
- Copy the entire code block.
- Paste it into a new file.
- Save the file with an
.html extension (e.g., weather_cards.html).
- Open the HTML file in a web browser.
You will see four weather cards, each displaying its respective animation continuously.