好的,这是一个包含 CSS 和 JavaScript 的单个 HTML 文件,用于生成一个动画天气卡片,并具有在不同天气状况之间切换的功能:
<!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: sans-serif;
background-color: #222;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
}
.weather-container {
display: flex;
gap: 20px;
}
.weather-card {
width: 200px;
height: 200px;
background-color: #333;
border-radius: 10px;
overflow: hidden;
position: relative;
}
.weather-icon {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100px;
height: 100px;
}
/* Weather-specific styles */
.sunny .weather-icon {
background: radial-gradient(circle, #FFD700, #FFA500);
animation: shine 2s infinite;
}
.rainy .weather-icon {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><path d="M50 10 L10 50 L90 50 Z" fill="#4285F4" /><path d="M50 20 L20 60 L80 60 Z" fill="#4285F4" /><path d="M50 30 L30 70 L70 70 Z" fill="#4285F4" /></svg>');
animation: rain 1s linear infinite;
}
.windy .weather-icon {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><path d="M50 10 L10 50 L90 50 Z" fill="#81D4FA" /><path d="M50 20 L20 60 L80 60 Z" fill="#81D4FA" /><path d="M50 30 L30 70 L70 70 Z" fill="#81D4FA" /></svg>');
animation: wind 2s linear infinite;
}
.snowy .weather-icon {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><path d="M50 10 L10 50 L90 50 Z" fill="#E1F5FE" /><path d="M50 20 L20 60 L80 60 Z" fill="#E1F5FE" /><path d="M50 30 L30 70 L70 70 Z" fill="#E1F5FE" /></svg>');
animation: snow 1.5s linear infinite;
}
/* Animations */
@keyframes shine {
0% { opacity: 0.7; }
50% { opacity: 1; }
100% { opacity: 0.7; }
}
@keyframes rain {
0% { transform: translateY(-100px); }
100% { transform: translateY(100px); }
}
@keyframes wind {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@keyframes snow {
0% { transform: translateY(-50px) rotate(0deg); }
100% { transform: translateY(50px) rotate(360deg); }
}
/* Button styles */
.button-container {
margin-top: 20px;
}
button {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin-right: 10px;
}
button:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<div class="weather-container">
<div class="weather-card sunny">
<div class="weather-icon"></div>
</div>
<div class="weather-card rainy">
<div class="weather-icon"></div>
</div>
<div class="weather-card windy">
<div class="weather-icon"></div>
</div>
<div class="weather-card snowy">
<div class="weather-icon"></div>
</div>
</div>
<div class="button-container">
<button onclick="changeWeather('sunny')">Sunny</button>
<button onclick="changeWeather('rainy')">Rainy</button>
<button onclick="changeWeather('windy')">Windy</button>
<button onclick="changeWeather('snowy')">Snowy</button>
</div>
<script>
const weatherCards = document.querySelectorAll('.weather-card');
function changeWeather(weatherType) {
weatherCards.forEach(card => {
card.classList.remove('sunny', 'rainy', 'windy', 'snowy');
card.classList.add(weatherType);
});
}
</script>
</body>
</html>
代码说明:
-
HTML 结构:
weather-container:包含所有天气卡片的容器。
weather-card:每个天气卡片。
weather-icon:卡片内的图标,用于显示天气动画。
button-container:包含用于切换天气的按钮。
-
CSS 样式:
- 基本样式:设置背景颜色、字体、对齐方式等。
weather-card:设置卡片的宽度、高度、背景颜色、圆角和溢出隐藏。
weather-icon:设置图标的位置和大小。
- 天气特定样式:为每种天气状况定义不同的背景和动画。
- 动画:使用
@keyframes 定义动画效果,例如 shine(阳光闪耀)、rain(雨滴下落)、wind(风旋转)和 snow(雪花飘落)。
- 按钮样式:设置按钮的样式。
-
JavaScript 代码:
changeWeather(weatherType) 函数:
- 获取所有天气卡片。
- 遍历每个卡片,移除所有天气类名。
- 添加指定的
weatherType 类名。
如何使用:
- 将代码保存为 HTML 文件(例如
weather.html)。
- 在浏览器中打开该 HTML 文件。
- 点击按钮即可在不同的天气状况之间切换。
改进方向:
- 更复杂的动画: 可以使用更复杂的 CSS 动画或 JavaScript 动画库(例如 GreenSock)来创建更逼真的动画效果。
- 动态数据: 可以从 API 获取实际的天气数据,并根据数据动态更新天气卡片。
- 用户界面: 可以添加更多的用户界面元素,例如温度显示、湿度显示等。
- 响应式设计: 可以使用媒体查询来使天气卡片在不同设备上都能良好显示。
- SVG 图标: 使用 SVG 图标可以获得更好的缩放效果和更清晰的图像。
- 性能优化: 对于复杂的动画,需要注意性能优化,例如使用
requestAnimationFrame 来提高动画流畅度。
希望这个示例代码能够帮助你创建动画天气卡片!