以下是一个包含 HTML、CSS 和 JavaScript 的完整代码示例,用于生成一个动画天气卡片。每个卡片通过独特的动画呈现风、雨、太阳和雪的天气状况,并且可以通过按钮切换不同的天气状态。
<!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 {
margin: 0;
font-family: Arial, sans-serif;
background-color: #121212;
color: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
}
.controls {
margin-bottom: 20px;
}
.controls button {
padding: 10px 20px;
margin: 5px;
border: none;
border-radius: 5px;
background-color: #4CAF50;
color: white;
cursor: pointer;
font-size: 16px;
}
.controls button:hover {
background-color: #45a049;
}
.weather-container {
display: flex;
gap: 20px;
}
.weather-card {
width: 200px;
height: 300px;
background-color: #212121;
border-radius: 10px;
position: relative;
overflow: hidden;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
}
.weather-card h2 {
text-align: center;
margin-top: 10px;
}
/* Wind Animation */
.wind .cloud {
position: absolute;
top: 50px;
left: -100px;
width: 100px;
height: 50px;
background-color: rgba(255, 255, 255, 0.7);
border-radius: 50px;
animation: wind-move 5s linear infinite;
}
@keyframes wind-move {
from {
transform: translateX(-100%);
}
to {
transform: translateX(200%);
}
}
/* Rain Animation */
.rain .drop {
position: absolute;
width: 2px;
height: 10px;
background-color: #87CEEB;
animation: rain-fall 1s linear infinite;
}
.rain .drop:nth-child(1) {
top: -10px;
left: 20px;
}
.rain .drop:nth-child(2) {
top: -20px;
left: 50px;
}
.rain .drop:nth-child(3) {
top: -30px;
left: 80px;
}
@keyframes rain-fall {
from {
transform: translateY(-100%);
}
to {
transform: translateY(300px);
}
}
/* Sun Animation */
.sun .sun-rays {
position: absolute;
top: 50%;
left: 50%;
width: 100px;
height: 100px;
background-color: yellow;
border-radius: 50%;
transform: translate(-50%, -50%);
box-shadow: 0 0 20px yellow;
animation: sun-shine 2s infinite alternate;
}
@keyframes sun-shine {
from {
box-shadow: 0 0 20px yellow;
}
to {
box-shadow: 0 0 40px yellow;
}
}
/* Snow Animation */
.snow .flake {
position: absolute;
width: 5px;
height: 5px;
background-color: white;
border-radius: 50%;
animation: snow-fall 3s linear infinite;
}
.snow .flake:nth-child(1) {
top: -10px;
left: 20px;
animation-delay: 0s;
}
.snow .flake:nth-child(2) {
top: -20px;
left: 50px;
animation-delay: 0.5s;
}
.snow .flake:nth-child(3) {
top: -30px;
left: 80px;
animation-delay: 1s;
}
@keyframes snow-fall {
from {
transform: translateY(-100%);
}
to {
transform: translateY(300px);
}
}
</style>
</head>
<body>
<div class="controls">
<button onclick="changeWeather('wind')">Wind</button>
<button onclick="changeWeather('rain')">Rain</button>
<button onclick="changeWeather('sun')">Sun</button>
<button onclick="changeWeather('snow')">Snow</button>
</div>
<div class="weather-container">
<div class="weather-card wind">
<h2>Wind</h2>
<div class="cloud"></div>
</div>
<div class="weather-card rain">
<h2>Rain</h2>
<div class="drop"></div>
<div class="drop"></div>
<div class="drop"></div>
</div>
<div class="weather-card sun">
<h2>Sun</h2>
<div class="sun-rays"></div>
</div>
<div class="weather-card snow">
<h2>Snow</h2>
<div class="flake"></div>
<div class="flake"></div>
<div class="flake"></div>
</div>
</div>
<script>
function changeWeather(weatherType) {
const cards = document.querySelectorAll('.weather-card');
cards.forEach(card => card.style.display = 'none');
switch (weatherType) {
case 'wind':
document.querySelector('.wind').style.display = 'block';
break;
case 'rain':
document.querySelector('.rain').style.display = 'block';
break;
case 'sun':
document.querySelector('.sun').style.display = 'block';
break;
case 'snow':
document.querySelector('.snow').style.display = 'block';
break;
}
}
// Show all weather cards initially
changeWeather('wind');
changeWeather('rain');
changeWeather('sun');
changeWeather('snow');
</script>
</body>
</html>
功能说明:
-
HTML:
- 每个天气卡片(
wind, rain, sun, snow)都有单独的容器。
- 提供一组按钮来切换不同的天气状态。
-
CSS:
- 使用
@keyframes 定义动画效果。
- 风:移动的云朵。
- 雨:下落的雨滴。
- 太阳:闪耀的光线。
- 雪:飘落的雪花。
-
JavaScript:
changeWeather 函数根据传入的天气类型显示对应的卡片,并隐藏其他卡片。
- 初始状态下所有卡片可见,便于展示效果。
将此代码保存为 .html 文件并在浏览器中打开即可查看效果!