好的,这是一个包含所有 HTML、CSS 和 JavaScript 的单一文件,用于创建四个并排展示的动画天气卡片。
每个卡片都代表一种天气状况(晴、雨、风、雪),并具有独特的、持续的动画效果。卡片采用深色主题。
您只需将以下所有代码复制并粘贴到一个名为 weather_cards.html 的文件中,然后用浏览器打开它即可。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>动画天气卡片</title>
<style>
/* --- 全局样式 --- */
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background-color: #1a1a2e; /* 深蓝紫色背景 */
color: #e0e0e0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
padding: 20px;
box-sizing: border-box;
}
/* --- 卡片容器 --- */
.container {
display: flex;
flex-wrap: wrap; /* 在小屏幕上换行 */
justify-content: center;
gap: 30px; /* 卡片之间的间距 */
}
/* --- 单个天气卡片的基础样式 --- */
.weather-card {
width: 260px;
height: 400px;
background: #2c3e50; /* 深蓝灰色卡片背景 */
border-radius: 20px;
position: relative; /* 子元素绝对定位的基准 */
overflow: hidden; /* 隐藏超出边界的动画元素 */
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
display: flex;
flex-direction: column;
justify-content: space-between; /* 标题和动画区域分开 */
border: 1px solid rgba(255, 255, 255, 0.1);
}
.weather-card h2 {
text-align: center;
margin: 20px 0;
font-size: 24px;
color: #ffffff;
z-index: 10; /* 确保标题在动画元素之上 */
text-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
}
.scene {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1; /* 动画场景在背景层 */
}
/* --- 1. 晴天卡片 (Sunny) --- */
.sunny {
background: linear-gradient(to top, #ffcc33, #ffb347); /* 温暖的渐变背景 */
}
.sunny .sun {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100px;
height: 100px;
background: #ffd700;
border-radius: 50%;
box-shadow: 0 0 50px #ffd700, 0 0 100px #ffdd77;
animation: pulse-sun 4s infinite ease-in-out;
}
.sunny .sun::before, .sunny .sun::after {
content: '';
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
background: #ffd700;
border-radius: 50%;
z-index: -1;
}
.sunny .sun::before {
animation: rotate-rays 8s infinite linear;
}
.sunny .sun::after {
animation: rotate-rays 8s infinite linear reverse;
}
@keyframes pulse-sun {
0%, 100% { transform: translate(-50%, -50%) scale(1); }
50% { transform: translate(-50%, -50%) scale(1.1); }
}
@keyframes rotate-rays {
from { transform: rotate(0deg) scale(1.3); opacity: 0.8; }
to { transform: rotate(360deg) scale(1.5); opacity: 0; }
}
/* --- 2. 雨天卡片 (Rainy) --- */
.rainy {
background: #34495e; /* 阴天的深蓝色 */
}
.rain-drop {
position: absolute;
bottom: 100%; /* 从顶部开始 */
width: 2px;
height: 15px;
background: linear-gradient(to bottom, rgba(255,255,255,0), rgba(174,194,224,0.7));
animation: fall 1s linear infinite;
}
.puddle {
position: absolute;
bottom: 0;
width: 100%;
height: 30px;
background: rgba(80, 100, 120, 0.5); /* 水坑颜色 */
border-top: 2px solid rgba(174,194,224,0.8);
}
.puddle-splash {
position: absolute;
bottom: 25px; /* 在水坑表面 */
width: 10px;
height: 5px;
background: transparent;
border: 2px solid rgba(174,194,224,0.8);
border-radius: 50%;
opacity: 0;
animation: splash 1s linear infinite;
}
@keyframes fall {
to {
transform: translateY(400px); /* 移动到卡片底部 */
}
}
@keyframes splash {
0% { transform: scale(0); opacity: 1; }
80% { transform: scale(2); opacity: 0; }
100% { opacity: 0; }
}
/* --- 3. 风天卡片 (Windy) --- */
.windy {
background: #7f8c8d; /* 风天的灰色 */
}
.wind-line {
position: absolute;
width: 50px;
height: 2px;
background: rgba(255, 255, 255, 0.7);
border-radius: 2px;
opacity: 0;
animation: blow-wind 2s ease-in-out infinite;
}
.cloud {
position: absolute;
width: 100px;
height: 35px;
background: #ecf0f1;
border-radius: 20px;
animation: move-cloud 10s linear infinite;
}
.cloud::before, .cloud::after {
content: '';
position: absolute;
background: #ecf0f1;
border-radius: 50%;
}
.cloud::before {
width: 50px; height: 50px;
top: -25px; left: 15px;
}
.cloud::after {
width: 40px; height: 40px;
top: -15px; right: 10px;
}
.cloud.back {
transform: scale(0.7);
filter: blur(2px);
animation-duration: 15s;
animation-delay: -5s;
}
@keyframes blow-wind {
0% { transform: translateX(-100px); opacity: 0; }
20%, 80% { opacity: 1; }
100% { transform: translateX(260px); opacity: 0; }
}
@keyframes move-cloud {
from { left: -150px; }
to { left: 260px; }
}
/* --- 4. 雪天卡片 (Snowy) --- */
.snowy {
background: #2c3a47; /* 冬夜的深蓝色 */
}
.snowflake {
position: absolute;
width: 8px;
height: 8px;
background: white;
border-radius: 50%;
opacity: 0.8;
animation: fall-snow 10s linear infinite;
}
.snow-ground {
position: absolute;
bottom: 0;
width: 100%;
height: 0; /* 初始无积雪 */
background: #f1f2f6;
animation: accumulate-snow 10s forwards; /* 动画结束后保持状态 */
animation-delay: 2s; /* 延迟积雪 */
}
@keyframes fall-snow {
0% {
transform: translateY(-50px) translateX(0);
}
100% {
transform: translateY(400px) translateX(50px);
}
}
@keyframes accumulate-snow {
to {
height: 20px;
}
}
</style>
</head>
<body>
<div class="container">
<!-- 晴天卡片 -->
<div class="weather-card sunny">
<h2>晴天</h2>
<div class="scene">
<div class="sun"></div>
</div>
</div>
<!-- 雨天卡片 -->
<div class="weather-card rainy">
<h2>雨天</h2>
<div class="scene" id="rainy-scene">
<!-- 雨滴和水花将由JS生成 -->
<div class="puddle"></div>
</div>
</div>
<!-- 风天卡片 -->
<div class="weather-card windy">
<h2>大风</h2>
<div class="scene" id="windy-scene">
<!-- 风线和云朵 -->
<div class="cloud" style="top: 80px;"></div>
<div class="cloud back" style="top: 120px;"></div>
</div>
</div>
<!-- 雪天卡片 -->
<div class="weather-card snowy">
<h2>下雪</h2>
<div class="scene" id="snowy-scene">
<!-- 雪花将由JS生成 -->
<div class="snow-ground"></div>
</div>
</div>
</div>
<script>
// 等待DOM加载完毕后执行脚本
document.addEventListener('DOMContentLoaded', function() {
/**
* 动态创建动画元素
* @param {string} sceneId - 目标场景的ID
* @param {string} elementClass - 要创建的元素的CSS类名
* @param {number} count - 创建的数量
* @param {function} [customizer] - (可选) 一个函数,用于对每个创建的元素进行自定义样式设置
*/
function createAnimatedElements(sceneId, elementClass, count, customizer) {
const scene = document.getElementById(sceneId);
if (!scene) return;
for (let i = 0; i < count; i++) {
const el = document.createElement('div');
el.className = elementClass;
// 设置随机的初始位置和动画延迟,使效果更自然
el.style.left = Math.random() * 100 + '%';
el.style.animationDelay = Math.random() * 5 + 's';
el.style.animationDuration = 1 + Math.random() * 0.5 + 's'; // 持续时间也随机化
// 如果提供了自定义函数,则调用它
if (customizer) {
customizer(el);
}
scene.appendChild(el);
}
}
// --- 为雨天卡片生成雨滴和水花 ---
createAnimatedElements('rainy-scene', 'rain-drop', 50);
createAnimatedElements('rainy-scene', 'puddle-splash', 5, (splash) => {
splash.style.animationDelay = Math.random() * 5 + 's';
splash.style.animationDuration = 1 + Math.random() * 0.5 + 's';
});
// --- 为风天卡片生成风线 ---
createAnimatedElements('windy-scene', 'wind-line', 15, (line) => {
line.style.top = 100 + Math.random() * 200 + 'px'; // 在卡片垂直居中区域随机分布
line.style.animationDelay = Math.random() * 2 + 's';
line.style.animationDuration = 1.5 + Math.random() * 1 + 's';
});
// --- 为雪天卡片生成雪花 ---
createAnimatedElements('snowy-scene', 'snowflake', 70, (flake) => {
const size = 2 + Math.random() * 6 + 'px';
flake.style.width = size;
flake.style.height = size;
flake.style.opacity = Math.random();
flake.style.animationDelay = Math.random() * 10 + 's';
flake.style.animationDuration = 5 + Math.random() * 5 + 's';
// 模拟雪花左右飘落
const horizontalMovement = -20 + Math.random() * 40 + 'px';
// 重新定义动画,使其独特
const animationName = `fall-snow-${Math.random().toString(36).substr(2, 5)}`;
const styleSheet = document.styleSheets[0];
const keyframes = `
@keyframes ${animationName} {
0% { transform: translateY(-50px) translateX(0); }
100% { transform: translateY(400px) translateX(${horizontalMovement}); }
}`;
styleSheet.insertRule(keyframes, styleSheet.cssRules.length);
flake.style.animationName = animationName;
});
});
</script>
</body>
</html>
代码解释
HTML 结构
<div class="container">: 这是一个 Flexbox 容器,用于将所有天气卡片并排居中显示。
<div class="weather-card">: 这是每张卡片的基础结构。它有四个变体,通过添加额外的类(.sunny, .rainy, .windy, .snowy)来应用不同的样式和动画。
<h2>: 显示天气状况的标题。
<div class="scene">: 这是一个容器,用于容纳所有动画元素(如雨滴、雪花、太阳等),确保它们在卡片内部正确地进行绝对定位。
- 特定元素:
- 晴天: 包含一个
.sun div,其光晕和旋转效果完全由 CSS 实现。
- 雨天: 包含一个
.puddle(水坑)div。雨滴 .rain-drop 和水花 .puddle-splash 是由 JavaScript 动态生成的。
- 风天: 包含两个
.cloud div(一个在前景,一个在背景)。风的线条 .wind-line 由 JavaScript 动态生成。
- 雪天: 包含一个
.snow-ground(积雪)div。雪花 .snowflake 由 JavaScript 动态生成。
CSS 样式
- 基础样式: 定义了页面背景、字体以及卡片容器的 Flexbox 布局。
- 卡片样式 (
.weather-card): 设置了卡片的尺寸、深色背景、圆角、阴影等。position: relative 和 overflow: hidden 是实现卡片内动画的关键。
- 天气特定样式:
.sunny: 使用温暖的渐变色背景。.sun 元素使用 box-shadow 制作发光效果,并通过 animation 实现脉冲和光线旋转的动画。
.rainy: 使用阴沉的深蓝色背景。.rain-drop 通过 animation 从上到下移动。.puddle-splash 则通过缩放和透明度变化模拟水花溅起的效果。
.windy: 使用灰色背景。.cloud 通过 ::before 和 ::after 伪元素组合成云朵形状,并使用 animation 水平移动。.wind-line 通过 animation 快速划过卡片。
.snowy: 使用冬夜的深蓝色背景。.snowflake 的下落动画结合了垂直和水平移动,模拟飘雪。.snow-ground 的高度通过 animation 逐渐增加,模拟积雪。
@keyframes: 定义了所有动画(如 pulse-sun, fall, blow-wind, fall-snow 等)的具体运动轨迹和属性变化。
JavaScript 逻辑
DOMContentLoaded 事件监听: 确保在 HTML 文档完全加载和解析后才执行脚本,防止因找不到元素而报错。
createAnimatedElements 函数:
- 这是一个可重用的辅助函数,用于简化动态创建元素的过程。
- 它接收场景 ID、元素类名、创建数量和一个可选的
customizer 函数。
- 在循环中,它创建指定数量的
div 元素。
- 为了使动画看起来自然,它为每个元素设置了随机的
animation-delay(动画延迟)、animation-duration(动画时长)和 left(水平位置)。
customizer 回调函数允许为特定类型的元素(如雪花)进行更复杂的随机化设置,比如大小和透明度。
- 元素生成调用:
- 脚本为雨天、风天和雪天卡片分别调用
createAnimatedElements 函数,动态地在对应的 .scene 容器中填充雨滴、风线和雪花。
- 对于雪花,它甚至动态地创建了独特的
@keyframes 规则,让每片雪花的飘落轨迹都略有不同,大大增强了真实感。