好的,下面是一个单一 HTML 文件,其中包含了实现您所描述的现代天气卡片所需的所有 HTML、CSS 和 JavaScript 代码。它采用了毛玻璃效果、深色背景和每种天气状态的 CSS 动画。
<!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 {
margin: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #1a1a2e 0%, #0f0f1e 100%); /* 深邃的渐变背景 */
color: #e0e0e0; /* 浅灰色文字 */
overflow: hidden; /* 防止页面滚动 */
position: relative; /* 用于绝对定位背景层 */
}
/* 天气卡片容器 */
.weather-card {
position: relative; /* For positioning children */
width: 350px;
height: 500px;
background: rgba(255, 255, 255, 0.1); /* 半透明白色背景 */
border-radius: 20px;
border: 1px solid rgba(255, 255, 255, 0.2); /* 细微的边框 */
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4), /* 深色阴影 */
inset 0 0 5px rgba(255, 255, 255, 0.1); /* 内发光效果 */
backdrop-filter: blur(15px); /* 毛玻璃效果 */
-webkit-backdrop-filter: blur(15px); /* Safari兼容 */
overflow: hidden; /* 隐藏溢出动画 */
display: flex;
flex-direction: column; /* 垂直布局 */
align-items: center;
justify-content: space-between; /* 内容分布 */
padding: 25px;
box-sizing: border-box; /* padding包含在width内 */
transition: all 0.5s ease-in-out; /* 平滑过渡 */
}
/* 背景动画层 */
.background-layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1; /* 放在内容后面 */
overflow: hidden; /* 确保动画元素不超出 */
}
/* 天气视觉元素容器 */
.weather-visual {
width: 150px;
height: 150px;
margin-top: 30px;
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
/* 天气图标(使用纯CSS绘制或占位) */
.weather-icon {
width: 100%;
height: 100%;
position: absolute;
opacity: 0.8;
/* 图标样式将在JS中动态添加 */
}
/* 天气信息 */
.weather-info {
text-align: center;
margin-top: 20px;
}
.temperature {
font-size: 3.5em; /* 较大字号 */
font-weight: 300; /* 细字体 */
letter-spacing: -2px; /* 轻微收紧字母间距 */
margin-bottom: 5px;
text-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
}
.location {
font-size: 1.4em;
font-weight: 500;
color: #ccc;
margin-bottom: 10px;
text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
.description {
font-size: 1.1em;
font-weight: 400;
color: #bbb;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}
/* 控制按钮 */
.controls {
width: 80%;
display: flex;
justify-content: space-around;
margin-top: 30px;
margin-bottom: 15px;
}
.controls button {
background: rgba(255, 255, 255, 0.2);
border: 1px solid rgba(255, 255, 255, 0.3);
color: #eee;
padding: 10px 18px;
border-radius: 25px; /* 圆角按钮 */
cursor: pointer;
font-size: 0.9em;
font-weight: 500;
transition: all 0.3s ease;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
outline: none; /* 去掉轮廓 */
}
.controls button:hover {
background: rgba(255, 255, 255, 0.4);
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
transform: translateY(-2px);
}
.controls button:active {
transform: translateY(0px);
background: rgba(255, 255, 255, 0.3);
}
/* 天气状态的特定样式和动画 */
/* --- 晴天 --- */
.weather-card.sunny .background-layer {
background: radial-gradient(circle, #ffcc66 10%, #ff9933 60%, #ff6600 100%); /* 暖色调 */
opacity: 0.4;
}
.weather-card.sunny .weather-icon::before {
content: ''; /* 太阳 */
position: absolute;
width: 80px;
height: 80px;
background: radial-gradient(circle, #fff 30%, #ffdd99 70%, #ffcc66 100%);
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-shadow: 0 0 20px #ffd700, 0 0 40px #ffcc00;
animation: pulse 2s infinite ease-in-out;
}
.weather-card.sunny .weather-icon::after {
content: ''; /* 太阳光线 */
position: absolute;
width: 6px;
height: 40px;
background: rgba(255, 223, 153, 0.7);
border-radius: 3px;
top: 50%;
left: 50%;
transform-origin: center 50%; /* 旋转中心 */
animation: sunrays 2s infinite linear;
}
.weather-card.sunny .weather-icon::after:nth-child(3) { animation-delay: -0.5s; transform: translate(-50%, -50%) rotate(30deg); }
.weather-card.sunny .weather-icon::after:nth-child(4) { animation-delay: -1s; transform: translate(-50%, -50%) rotate(60deg); }
.weather-card.sunny .weather-icon::after:nth-child(5) { animation-delay: -1.5s; transform: translate(-50%, -50%) rotate(90deg); }
/* --- 雨天 --- */
.weather-card.rainy .background-layer {
background: linear-gradient(135deg, #4b6cb7 0%, #182848 100%); /* 深蓝色调 */
opacity: 0.6;
}
.weather-card.rainy .weather-icon::before {
content: ''; /* 云 */
position: absolute;
width: 120px;
height: 70px;
background: rgba(100, 120, 160, 0.8);
border-radius: 50px;
top: 30%;
left: 50%;
transform: translate(-50%, -50%);
filter: blur(5px);
}
.weather-card.rainy .weather-icon::after {
content: ''; /* 雨滴 */
position: absolute;
width: 3px;
height: 15px;
background: rgba(70, 130, 180, 0.7);
border-radius: 50%;
top: 0; /* 从云上方开始 */
left: 50%;
opacity: 0;
animation: raindrop 1.5s linear infinite;
}
/* 生成多个雨滴 */
.weather-card.rainy .weather-icon::after:nth-of-type(2) { left: 40%; animation-delay: -0.3s; }
.weather-card.rainy .weather-icon::after:nth-of-type(3) { left: 60%; animation-delay: -0.6s; }
.weather-card.rainy .weather-icon::after:nth-of-type(4) { left: 30%; animation-delay: -0.9s; }
.weather-card.rainy .weather-icon::after:nth-of-type(5) { left: 70%; animation-delay: -1.2s; }
/* --- 下雪 --- */
.weather-card.snowy .background-layer {
background: linear-gradient(135deg, #6b7386 0%, #4b515e 100%); /* 灰色调 */
opacity: 0.7;
}
.weather-card.snowy .weather-icon {
background: radial-gradient(circle, rgba(180, 180, 180, 0.5) 30%, rgba(150, 150, 150, 0.3) 70%);
border-radius: 50%;
filter: blur(3px);
transform: scale(0.8);
}
.weather-card.snowy .weather-icon::before,
.weather-card.snowy .weather-icon::after { /* 雪花主体 */
content: '';
position: absolute;
width: 10px;
height: 10px;
background: transparent;
border: 2px solid rgba(200, 200, 220, 0.8);
border-radius: 50%;
top: 50%;
left: 50%;
opacity: 0;
animation: snowflake 3s linear infinite;
}
/* 雪花形状 (伪六边形) */
.weather-card.snowy .weather-icon::before {
width: 15px; height: 15px; border-width: 1px;
animation: snowflake 3s linear infinite, drift 4s linear infinite;
animation-delay: -0.5s;
transform: translate(-50%, -50%) scale(0.7);
}
.weather-card.snowy .weather-icon::after {
width: 12px; height: 12px; border-width: 1px;
animation: snowflake 3s linear infinite, drift 3s linear reverse infinite;
animation-delay: -1s;
transform: translate(-50%, -50%) scale(0.9);
}
/* 生成更多雪花 */
.weather-card.snowy .weather-icon > div {
position: absolute;
width: 8px;
height: 8px;
background: transparent;
border: 1px solid rgba(210, 210, 230, 0.7);
border-radius: 50%;
top: 50%;
left: 50%;
opacity: 0;
animation: snowflake 2.5s linear infinite, drift 3.5s linear infinite;
transform: translate(-50%, -50%);
}
.weather-card.snowy .weather-icon > div:nth-child(2) { animation-delay: -0.8s; transform: translate(-50%, -50%) scale(0.8); }
.weather-card.snowy .weather-icon > div:nth-child(3) { animation-delay: -1.6s; transform: translate(-50%, -50%) scale(1.1); animation-duration: 3.5s; }
.weather-card.snowy .weather-icon > div:nth-child(4) { animation-delay: -2.4s; transform: translate(-50%, -50%) scale(0.9); animation-duration: 2.8s; }
/* --- 刮风 --- */
.weather-card.windy .background-layer {
background: linear-gradient(135deg, #5f7c8a 0%, #3f515e 100%); /* 灰蓝色调 */
opacity: 0.5;
}
.weather-card.windy .weather-icon::before, /* 云 */
.weather-card.windy .weather-icon::after {
content: '';
position: absolute;
background: rgba(150, 170, 180, 0.6);
border-radius: 50%;
filter: blur(8px);
animation: wind-move 8s linear infinite alternate;
}
.weather-card.windy .weather-icon::before {
width: 160px;
height: 90px;
top: 40%;
left: 50%;
transform: translate(-50%, -50%) scale(1.2);
}
.weather-card.windy .weather-icon::after {
width: 130px;
height: 70px;
top: 55%;
left: 45%;
transform: translate(-50%, -50%) scale(0.8);
animation-duration: 6s;
animation-delay: -2s;
opacity: 0.8;
}
/* 飘动的叶子或粒子 */
.weather-card.windy .leaf {
position: absolute;
width: 10px;
height: 25px;
background: rgba(139, 69, 19, 0.6); /* 棕色 */
border-radius: 40% 40% 0 0;
top: 50%;
left: 50%;
opacity: 0;
animation: leaf-float 5s linear infinite;
}
.weather-card.windy .leaf::before {
content: '';
position: absolute;
width: 10px;
height: 10px;
background: rgba(139, 69, 19, 0.4);
border-radius: 50%;
bottom: -5px;
left: 0;
filter: blur(3px);
}
/* 生成多个叶子 */
.weather-card.windy .leaf:nth-child(1) { animation-delay: -0.5s; transform: translate(-100px, -50px) rotate(-20deg); }
.weather-card.windy .leaf:nth-child(2) { animation-delay: -1.5s; transform: translate(80px, -80px) rotate(30deg); }
.weather-card.windy .leaf:nth-child(3) { animation-delay: -2.5s; transform: translate(-120px, 20px) rotate(-10deg); }
.weather-card.windy .leaf:nth-child(4) { animation-delay: -3.5s; transform: translate(100px, 50px) rotate(25deg); }
.weather-card.windy .leaf:nth-child(5) { animation-delay: -4.5s; transform: translate(-80px, 80px) rotate(-35deg); }
/* --- 关键帧动画 --- */
/* 太阳脉动 */
@keyframes pulse {
0%, 100% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
50% { transform: translate(-50%, -50%) scale(1.05); opacity: 0.9; }
}
/* 太阳光线旋转 */
@keyframes sunrays {
0%, 100% { transform: translate(-50%, -50%) rotate(0deg) scaleY(1); opacity: 0.7; }
50% { transform: translate(-50%, -50%) rotate(180deg) scaleY(1.2); opacity: 0.5; }
}
/* 雨滴下落 */
@keyframes raindrop {
0% { transform: translateY(-50px) scale(0.8); opacity: 0; }
20% { opacity: 1; }
100% { transform: translateY(250px) scale(1); opacity: 0; } /* 下落到底部 */
}
/* 雪花下落 */
@keyframes snowflake {
0% { transform: translate(-50%, -50%) scale(0.5); opacity: 0; }
10% { opacity: 1; }
100% { transform: translate(-50%, 250px) scale(1); opacity: 0; }
}
/* 雪花漂移 */
@keyframes drift {
0%, 100% { transform: translate(-50%, -50%) scale(0.9) translateX(0px); }
50% { transform: translate(-50%, -50%) scale(0.9) translateX(20px); }
}
/* 云朵移动 */
@keyframes wind-move {
0% { transform: translate(-50%, -50%) translateX(-50px); opacity: 0.7; }
100% { transform: translate(-50%, -50%) translateX(50px); opacity: 0.9; }
}
/* 叶子飘落 */
@keyframes leaf-float {
0% { transform: translate(-100px, -50px) rotate(-20deg) translateY(-50px); opacity: 0; }
30% { opacity: 0.6; }
100% { transform: translate(-100px, -50px) rotate(-20deg) translateY(250px); opacity: 0; }
}
</style>
</head>
<body>
<div class="weather-card" id="weather-card">
<div class="background-layer" id="background-layer"></div>
<div class="weather-visual">
<div class="weather-icon" id="weather-icon">
<!-- 雪花动画的额外div -->
<div id="snow-extra1"></div>
<div id="snow-extra2"></div>
<div id="snow-extra3"></div>
<div id="snow-extra4"></div>
</div>
</div>
<div class="weather-info">
<div class="temperature" id="temperature">--°C</div>
<div class="location" id="location">Loading...</div>
<div class="description" id="description">--</div>
</div>
<div class="controls">
<button data-weather="sunny" aria-label="切换到晴天">☀️ 晴天</button>
<button data-weather="rainy" aria-label="切换到雨天">🌧️ 雨天</button>
<button data-weather="snowy" aria-label="切换到下雪">❄️ 下雪</button>
<button data-weather="windy" aria-label="切换到刮风">💨 刮风</button>
</div>
</div>
<script>
// --- DOM Elements ---
const card = document.getElementById('weather-card');
const backgroundLayer = document.getElementById('background-layer');
const weatherIconContainer = document.getElementById('weather-icon');
const temperatureDisplay = document.getElementById('temperature');
const locationDisplay = document.getElementById('location');
const descriptionDisplay = document.getElementById('description');
const controlButtons = document.querySelectorAll('.controls button');
// --- Weather Data ---
const weatherData = {
sunny: {
temp: "25°C",
location: "阳光海岸",
description: "晴朗",
bgClass: "sunny",
iconAnimation: () => { /* 太阳动画由CSS ::before 和 ::after 实现 */ }
},
rainy: {
temp: "15°C",
location: "雨城",
description: "小雨",
bgClass: "rainy",
iconAnimation: () => { /* 雨滴动画由CSS ::after 实现 */ }
},
snowy: {
temp: "-2°C",
location: "雪域镇",
description: "小雪",
bgClass: "snowy",
iconAnimation: () => { /* 雪花动画由CSS ::before/::after 及额外div实现 */ }
},
windy: {
temp: "18°C",
location: "风之谷",
description: "多云",
bgClass: "windy",
iconAnimation: () => { /* 云和叶子动画由CSS ::before/::after 及叶子div实现 */ }
}
};
// --- State Management ---
let currentWeather = 'sunny'; // Initial state
// --- Functions ---
// 清除所有动画元素和类
function clearWeatherEffects() {
// 移除所有天气状态类
card.classList.remove('sunny', 'rainy', 'snowy', 'windy');
// 清理雪花和叶子的额外元素 (如果存在)
const extraSnowflakes = weatherIconContainer.querySelectorAll('div');
extraSnowflakes.forEach(el => el.remove());
// 重置背景层样式 (如果需要更复杂的重置)
backgroundLayer.style.cssText = ''; // Reset inline styles
}
// 更新卡片显示
function updateWeatherDisplay(newState) {
clearWeatherEffects();
const data = weatherData[newState];
// 更新信息文本
temperatureDisplay.textContent = data.temp;
locationDisplay.textContent = data.location;
descriptionDisplay.textContent = data.description;
// 应用新的天气状态类到卡片
card.classList.add(data.bgClass);
// 执行特定天气状态的动画设置 (如果CSS无法完全满足)
if (data.iconAnimation) {
data.iconAnimation();
}
// 特殊处理:为下雪和刮风添加动态元素
if (newState === 'snowy') {
// 雪花动画主要由 CSS 控制,但可以增加更多元素用于动画
// (HTML中已预留额外 div,CSS根据类名应用动画)
} else if (newState === 'windy') {
// 刮风添加叶子动画
// (HTML中已预留叶子div,CSS根据类名应用动画)
}
}
// --- Event Listeners ---
controlButtons.forEach(button => {
button.addEventListener('click', () => {
const newState = button.getAttribute('data-weather');
if (newState !== currentWeather) {
currentWeather = newState;
updateWeatherDisplay(currentWeather);
}
});
});
// --- Initial Setup ---
// Set initial state on load
updateWeatherDisplay(currentWeather);
// Optional: Add slight interaction effect to the card itself
document.addEventListener('mousemove', (e) => {
const cardRect = card.getBoundingClientRect();
const cardCenterX = cardRect.left + cardRect.width / 2;
const cardCenterY = cardRect.top + cardRect.height / 2;
const mouseX = e.clientX - cardCenterX;
const mouseY = e.clientY - cardCenterY;
// Tilt effect based on mouse position
const tiltX = (mouseX / (cardRect.width / 2)) * 15; // Max 15 degrees tilt
const tiltY = -(mouseY / (cardRect.height / 2)) * 15; // Max 15 degrees tilt
card.style.transform = `perspective(1000px) rotateX(${tiltY}deg) rotateY(${tiltX}deg)`;
// Subtle background shift
backgroundLayer.style.transform = `translateX(${mouseX * 0.05}px) translateY(${mouseY * 0.05}px)`;
});
document.addEventListener('mouseleave', () => {
card.style.transform = 'perspective(1000px) rotateX(0deg) rotateY(0deg)';
backgroundLayer.style.transform = 'none';
});
</script>
</body>
</html>
代码说明:
-
HTML 结构:
- 有一个主要的
.weather-card 容器,包含了背景层、视觉元素区 (.weather-visual)、信息区 (.weather-info) 和控制按钮区 (.controls)。
.background-layer 是一个绝对定位的元素,用于承载根据天气变化的主要背景色和动画。
.weather-icon 是放置天气视觉效果(如太阳、云、雪花)的容器。内部的 div 是为下雪和刮风天气添加多个动画元素的预留位置。
- 信息区 (
temperature, location, description) 使用 ID 方便 JavaScript 更新。
- 控制区包含四个按钮,每个按钮有一个
data-weather 属性来标识对应的天气状态。
-
CSS 样式:
- 整体:
body 使用深色渐变背景,并通过 Flexbox 将卡片居中。
- 卡片 (
.weather-card):
- 使用
rgba 设置半透明的白色背景,营造玻璃质感。
border 和 box-shadow 增加层次感和精致度。
backdrop-filter: blur(15px) 是实现毛玻璃效果的关键。
overflow: hidden 防止动画元素超出卡片边界。
- 使用
position: relative 和 z-index: -1 将 .background-layer 放在内容后面。
- 天气动画:
- 每种天气状态(
sunny, rainy, snowy, windy)通过给 .weather-card 添加对应类名来激活。
- 晴天: 背景使用暖色径向渐变,太阳通过
::before 伪元素绘制(带脉动和发光效果),光线用 ::after 伪元素实现旋转动画。
- 雨天: 背景使用深蓝色渐变,云用
::before 伪元素模拟,雨滴用多个 ::after 伪元素实现下落和消失的动画。
- 下雪: 背景为灰色调,中心元素(雪花)使用带模糊和漂移动画的伪元素和
div 实现,模拟雪花飘落和侧移。
- 刮风: 背景为灰蓝色调,云朵用伪元素实现水平移动,并添加了
.leaf 元素,模拟树叶随风飘动的效果。
- 动画关键帧: 使用
@keyframes 定义了 pulse(脉动)、sunrays(光线旋转)、raindrop(雨滴下落)、snowflake(雪花下落)、drift(雪花漂移)、wind-move(云朵移动)、leaf-float(叶子飘落)等动画。
- 按钮: 样式现代,带有悬停和激活效果。
-
JavaScript 逻辑:
- 获取所有必要的 DOM 元素。
- 定义
weatherData 对象,存储每种天气状态下的文本信息和用于添加的 CSS 类名。
clearWeatherEffects() 函数用于移除旧的天气状态类和动画元素,为切换做准备。
如何运行:
- 将所有代码(从
<!DOCTYPE html> 到 </script> 标签)复制到一个文本文件中。
- 将文件保存为
.html 扩展名(例如 weather_card.html)。
- 用您的网页浏览器(如 Chrome, Firefox, Edge 等)打开这个 HTML 文件。
您将看到一个具有毛玻璃效果的深色天气卡片,并且可以通过底部的按钮在四种天气状态之间切换,每种状态都有独特的 CSS 动画和视觉效果。