Certainly! Below is a single HTML file containing embedded CSS and JavaScript to create an animated weather card that visually represents different weather conditions: Wind, Rain, Sun, and Snow. Each card is displayed side by side with distinct animations for each weather type. The JavaScript includes functions to switch between the different weather conditions, demonstrating the animations for each.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<title>Animated Weather Cards</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #333;
}
.weather-container {
display: flex;
justify-content: space-around;
width: 80%;
}
.weather-card {
width: 200px;
height: 300px;
background-color: #444;
border-radius: 10px;
margin: 10px;
position: relative;
overflow: hidden;
color: white;
text-align: center;
padding: 20px;
}
.weather-card h2 {
margin: 0;
font-size: 24px;
}
.weather-card p {
font-size: 18px;
}
.weather-card .animation {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.weather-card .animation .cloud {
position: absolute;
top: 20%;
left: 50%;
width: 100px;
height: 60px;
background-color: #bbb;
border-radius: 50%;
animation: moveClouds 10s linear infinite;
}
.weather-card .animation .raindrop {
position: absolute;
top: 0;
left: 50%;
width: 5px;
height: 10px;
background-color: #00f;
border-radius: 50%;
animation: fallRain 1s linear infinite;
}
.weather-card .animation .sun {
position: absolute;
top: 10%;
left: 50%;
width: 80px;
height: 80px;
background-color: #ff0;
border-radius: 50%;
box-shadow: 0 0 20px rgba(255, 255, 0, 0.5);
animation: shineSun 5s linear infinite;
}
.weather-card .animation .snowflake {
position: absolute;
top: 0;
left: 50%;
width: 5px;
height: 5px;
background-color: #fff;
border-radius: 50%;
animation: fallSnow 3s linear infinite;
}
@keyframes moveClouds {
0% { transform: translateX(-100%); }
100% { transform: translateX(100%); }
}
@keyframes fallRain {
0% { transform: translateY(-100%); }
100% { transform: translateY(100%); }
}
@keyframes shineSun {
0% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.1); opacity: 0.8; }
100% { transform: scale(1); opacity: 1; }
}
@keyframes fallSnow {
0% { transform: translateY(-100%); }
100% { transform: translateY(100%); }
}
</style>
</head>
<body>
<div class="weather-container">
<div class="weather-card" id="wind">
<h2>Wind</h2>
<p>Strong winds are blowing</p>
<div class="animation">
<div class="cloud"></div>
</div>
</div>
<div class="weather-card" id="rain">
<h2>Rain</h2>
<p>Heavy rainfall expected</p>
<div class="animation">
<div class="raindrop"></div>
</div>
</div>
<div class="weather-card" id="sun">
<h2>Sun</h2>
<p>Clear and sunny skies</p>
<div class="animation">
<div class="sun"></div>
</div>
</div>
<div class="weather-card" id="snow">
<h2>Snow</h2>
<p>Snowfall in the forecast</p>
<div class="animation">
<div class="snowflake"></div>
</div>
</div>
</div>
<script>
// Function to switch between weather conditions
function switchWeather(weatherType) {
const weatherCards = document.querySelectorAll('.weather-card');
weatherCards.forEach(card => {
card.style.display = 'none';
});
const activeCard = document.getElementById(weatherType);
activeCard.style.display = 'block';
}
// Initialize with 'wind' weather
switchWeather('wind');
// Example: Switch to 'rain' after 5 seconds
setTimeout(() => {
switchWeather('rain');
}, 5000);
// Example: Switch to 'sun' after 10 seconds
setTimeout(() => {
switchWeather('sun');
}, 10000);
// Example: Switch to 'snow' after 15 seconds
setTimeout(() => {
switchWeather('snow');
}, 15000);
</script>
</body>
</html>
Explanation:
-
HTML Structure: The page contains a weather-container div that holds four weather-card divs, each representing a different weather condition: Wind, Rain, Sun, and Snow. Each card has a title, a description, and an animation div that holds the respective animated elements.
-
CSS Styling: The cards are styled with a dark background and positioned side by side using Flexbox. Each card has a distinct animation:
- Wind: A cloud element moves horizontally across the card.
- Rain: Raindrops fall vertically from the top to the bottom of the card.
- Sun: A sun element shines with a pulsating effect.
- Snow: Snowflakes fall vertically from the top to the bottom of the card.
-
JavaScript Functionality: The switchWeather function hides all weather cards and displays the selected one. The script initializes with the 'wind' weather and switches to