<!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 {
background-color: #333;
font-family: Arial, sans-serif;
}
.weather-card {
width: 200px;
height: 200px;
background-color: #444;
margin: 10px;
display: inline-block;
position: relative;
}
.wind {
background-image: linear-gradient(to bottom, #555, #666);
}
.rain {
background-image: linear-gradient(to bottom, #666, #777);
}
.sun {
background-image: linear-gradient(to bottom, #888, #999);
}
.snow {
background-image: linear-gradient(to bottom, #999, #aaa);
}
.cloud {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 50px;
height: 50px;
background-color: #fff;
border-radius: 50%;
}
.wind .cloud {
animation: move-cloud 5s infinite;
}
@keyframes move-cloud {
0% {
transform: translate(-50%, -50%) translateX(0);
}
50% {
transform: translate(-50%, -50%) translateX(100px);
}
100% {
transform: translate(-50%, -50%) translateX(0);
}
}
.rain .raindrop {
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
width: 5px;
height: 5px;
background-color: #fff;
border-radius: 50%;
animation: fall-raindrop 2s infinite;
}
@keyframes fall-raindrop {
0% {
top: 0;
}
100% {
top: 100%;
}
}
.sun .ray {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) rotate(45deg);
width: 50px;
height: 5px;
background-color: #fff;
}
.sun .ray:nth-child(1) {
transform: translate(-50%, -50%) rotate(45deg);
}
.sun .ray:nth-child(2) {
transform: translate(-50%, -50%) rotate(135deg);
}
.sun .ray:nth-child(3) {
transform: translate(-50%, -50%) rotate(225deg);
}
.sun .ray:nth-child(4) {
transform: translate(-50%, -50%) rotate(315deg);
}
.snow .snowflake {
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
width: 10px;
height: 10px;
background-color: #fff;
border-radius: 50%;
animation: fall-snowflake 3s infinite;
}
@keyframes fall-snowflake {
0% {
top: 0;
}
100% {
top: 100%;
}
}
</style>
</head>
<body>
<div class="weather-card wind">
<div class="cloud"></div>
</div>
<div class="weather-card rain">
<div class="raindrop"></div>
<div class="raindrop" style="animation-delay: 0.5s;"></div>
<div class="raindrop" style="animation-delay: 1s;"></div>
</div>
<div class="weather-card sun">
<div class="ray"></div>
<div class="ray"></div>
<div class="ray"></div>
<div class="ray"></div>
</div>
<div class="weather-card snow">
<div class="snowflake"></div>
<div class="snowflake" style="animation-delay: 0.5s;"></div>
<div class="snowflake" style="animation-delay: 1s;"></div>
</div>
<button onclick="switchWeather('wind')">Wind</button>
<button onclick="switchWeather('rain')">Rain</button>
<button onclick="switchWeather('sun')">Sun</button>
<button onclick="switchWeather('snow')">Snow</button>
<script>
function switchWeather(weather) {
const cards = document.querySelectorAll('.weather-card');
cards.forEach(card => {
card.classList.remove('wind', 'rain', 'sun', 'snow');
if (weather === 'wind') {
card.innerHTML = '<div class="cloud"></div>';
card.classList.add('wind');
} else if (weather === 'rain') {
card.innerHTML = `
<div class="raindrop"></div>
<div class="raindrop" style="animation-delay: 0.5s;"></div>
<div class="raindrop" style="animation-delay: 1s;"></div>
`;
card.classList.add('rain');
} else if (weather === 'sun') {
card.innerHTML = `
<div class="ray"></div>
<div class="ray"></div>
<div class="ray"></div>
<div class="ray"></div>
`;
card.classList.add('sun');
} else if (weather === 'snow') {
card.innerHTML = `
<div class="snowflake"></div>
<div class="snowflake" style="animation-delay: 0.5s;"></div>
<div class="snowflake" style="animation-delay: 1s;"></div>
`;
card.classList.add('snow');
}
});
}
</script>
</body>
</html>
This code creates four weather cards with different animations for wind, rain, sun, and snow. The switchWeather function allows you to switch between the different weather conditions by adding or removing classes from the cards and updating their inner HTML. The animations are created using CSS keyframes and the animation property.