Here's a simple example of how you can create an animated weather card using HTML, CSS, and JavaScript. This example includes animations for wind, rain, sun, and snow.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<title>Animated Weather Card</title>
<style>
/* CSS Styles */
body {
background-color: #333;
display: flex;
justify-content: space-around;
align-items: center;
height: 100vh;
}
.weather-card {
width: 200px;
height: 200px;
background-color: #444;
border-radius: 10px;
position: relative;
overflow: hidden;
}
.wind {
background-image: linear-gradient(to bottom, #555, #666);
}
.rain {
background-image: linear-gradient(to bottom, #444, #555);
}
.sun {
background-image: linear-gradient(to bottom, #ffcc00, #ffff00);
}
.snow {
background-image: linear-gradient(to bottom, #ccc, #fff);
}
.cloud {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #fff;
opacity: 0.5;
animation: moveCloud 5s infinite linear;
}
@keyframes moveCloud {
0% {
transform: translateX(0);
}
100% {
transform: translateX(100%);
}
}
.raindrop {
position: absolute;
width: 5px;
height: 5px;
background-color: #000;
border-radius: 50%;
animation: fallRain 2s infinite linear;
}
@keyframes fallRain {
0% {
top: 0;
}
100% {
top: 100%;
}
}
.sun-ray {
position: absolute;
width: 50px;
height: 50px;
background-color: #ffff00;
border-radius: 50%;
animation: shineSun 2s infinite linear;
}
@keyframes shineSun {
0% {
opacity: 0.5;
}
50% {
opacity: 1;
}
100% {
opacity: 0.5;
}
}
.snowflake {
position: absolute;
width: 10px;
height: 10px;
background-color: #fff;
border-radius: 50%;
animation: fallSnow 3s infinite linear;
}
@keyframes fallSnow {
0% {
top: 0;
}
100% {
top: 100%;
}
}
button {
margin-top: 20px;
}
</style>
</head>
<body>
<div class="weather-card wind">
<div class="cloud"></div>
<div class="wind-lines"></div>
</div>
<div class="weather-card rain">
<div class="cloud"></div>
<div class="raindrop" style="left: 10%; top: 10%;"></div>
<div class="raindrop" style="left: 30%; top: 20%;"></div>
<div class="raindrop" style="left: 50%; top: 30%;"></div>
</div>
<div class="weather-card sun">
<div class="sun-ray" style="left: 25%; top: 25%;"></div>
<div class="sun-ray" style="left: 50%; top: 50%;"></div>
<div class="sun-ray" style="left: 75%; top: 75%;"></div>
</div>
<div class="weather-card snow">
<div class="snowflake" style="left: 10%; top: 10%;"></div>
<div class="snowflake" style="left: 30%; top: 20%;"></div>
<div class="snowflake" style="left: 50%; top: 30%;"></div>
</div>
<button onclick="changeWeather('wind')">Wind</button>
<button onclick="changeWeather('rain')">Rain</button>
<button onclick="changeWeather('sun')">Sun</button>
<button onclick="changeWeather('snow')">Snow</button>
<script>
// JavaScript Code
function changeWeather(weatherType) {
const cards = document.querySelectorAll('.weather-card');
cards.forEach(card => {
card.classList.remove('wind', 'rain', 'sun', 'snow');
});
cards.classList.add(weatherType);
cards<a href="https://codepen.io/alexandrevacassin/pen/wvbbJXm" style="color: #0366d6; text-decoration: none;">[1]</a>.classList.add(weatherType);
cards<a href="https://www.youtube.com/watch?v=MIYQR-Ybrn4" style="color: #0366d6; text-decoration: none;">[2]</a>.classList.add(weatherType);
cards<a href="https://www.youtube.com/watch?v=RbJRjp6eHBU" style="color: #0366d6; text-decoration: none;">[3]</a>.classList.add(weatherType);
}
</script>
</body>
</html>
Explanation
-
HTML Structure: The HTML includes four div elements with the class weather-card, each representing a different weather condition. There are also buttons to switch between these conditions.
-
CSS Styles: The CSS defines the styles for each weather condition, including animations for moving clouds, falling raindrops, shining sun rays, and falling snowflakes.
-
JavaScript: The JavaScript function changeWeather allows users to switch between different weather conditions by adding or removing classes from the weather cards.
Improvements
- Dynamic Rain and Snow: To make the rain and snow more dynamic, you could generate the raindrops and snowflakes programmatically using JavaScript, allowing for random positions and animations.
- Wind Lines: For the wind condition, you could add animated lines to simulate wind movement.
- Background Adjustments: Adjust the background colors or gradients to better match each weather condition.
This example provides a basic framework. You can enhance it by adding more complex animations or integrating with weather APIs for real-time data.