<!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>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Arial', sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #1a1a2e;
transition: background 1.5s ease;
overflow: hidden;
}
/* 卡片容器 */
.weather-card {
position: relative;
width: 350px;
height: 500px;
background: rgba(20, 20, 40, 0.7);
border-radius: 24px;
backdrop-filter: blur(10px);
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.5);
overflow: hidden;
display: flex;
flex-direction: column;
justify-content: space-between;
padding: 30px;
z-index: 1;
transition: all 0.5s ease;
}
/* 毛玻璃效果 */
.weather-card::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(to bottom right, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.05));
border-radius: 24px;
z-index: -1;
}
/* 卡片顶部 */
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
.location {
color: #fff;
font-size: 1.2rem;
font-weight: 500;
}
.date {
color: rgba(255, 255, 255, 0.7);
font-size: 0.9rem;
}
/* 天气图标和温度 */
.weather-main {
text-align: center;
padding: 20px 0;
}
.weather-icon {
width: 120px;
height: 120px;
margin: 0 auto;
position: relative;
}
.temperature {
font-size: 3.5rem;
font-weight: 700;
color: #fff;
margin: 10px 0;
text-shadow: 0 0 10px rgba(255, 255, 255, 0.3);
}
.weather-status {
font-size: 1.5rem;
color: rgba(255, 255, 255, 0.9);
margin-bottom: 10px;
}
/* 天气详情 */
.weather-details {
display: flex;
justify-content: space-between;
background: rgba(255, 255, 255, 0.1);
border-radius: 16px;
padding: 15px;
margin-top: 20px;
}
.detail {
display: flex;
flex-direction: column;
align-items: center;
}
.detail-label {
color: rgba(255, 255, 255, 0.6);
font-size: 0.8rem;
margin-bottom: 5px;
}
.detail-value {
color: #fff;
font-size: 1.1rem;
font-weight: 500;
}
/* 切换按钮 */
.weather-switcher {
display: flex;
justify-content: center;
gap: 15px;
margin-top: 25px;
}
.weather-btn {
width: 60px;
height: 60px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.1);
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
transition: all 0.3s ease;
border: none;
outline: none;
color: white;
}
.weather-btn:hover {
transform: translateY(-5px);
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}
.weather-btn.active {
background: rgba(255, 255, 255, 0.25);
box-shadow: 0 0 15px rgba(255, 255, 255, 0.5);
}
/* 各种天气的动画元素 */
.animation-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: -1;
overflow: hidden;
}
/* 晴天动画 */
.sun {
position: absolute;
width: 80px;
height: 80px;
background: #ffde59;
border-radius: 50%;
top: 60px;
left: 50%;
transform: translateX(-50%);
box-shadow: 0 0 50px #ff9d00;
opacity: 0;
transition: opacity 1s ease;
}
.sun-ray {
position: absolute;
top: 50%;
left: 50%;
width: 120px;
height: 120px;
border-radius: 50%;
background: transparent;
opacity: 0;
box-shadow: 0 0 60px 30px rgba(255, 214, 0, 0.7);
animation: pulse 3s infinite;
}
@keyframes pulse {
0%, 100% {
transform: translate(-50%, -50%) scale(0.8);
}
50% {
transform: translate(-50%, -50%) scale(1.1);
}
}
/* 雨天动画 */
.rain-drop {
position: absolute;
width: 2px;
height: 20px;
background: linear-gradient(to bottom, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0.7));
border-radius: 0 0 5px 5px;
opacity: 0;
animation: rain-fall 1.5s linear infinite;
}
@keyframes rain-fall {
0% {
transform: translateY(-100px);
opacity: 0;
}
20% {
opacity: 1;
}
100% {
transform: translateY(500px);
opacity: 0.3;
}
}
/* 下雪动画 */
.snowflake {
position: absolute;
width: 8px;
height: 8px;
background: white;
border-radius: 50%;
opacity: 0;
animation: snow-fall 10s linear infinite;
}
@keyframes snow-fall {
0% {
transform: translateY(-100px) translateX(0) rotate(0deg);
opacity: 0;
}
20% {
opacity: 0.8;
}
100% {
transform: translateY(500px) translateX(100px) rotate(360deg);
opacity: 0.2;
}
}
/* 刮风动画 */
.leaf {
position: absolute;
width: 15px;
height: 15px;
background: rgba(139, 195, 74, 0.7);
border-radius: 30% 70% 70% 30% / 30% 30% 70% 70%;
opacity: 0;
animation: leaf-blow 8s linear infinite;
}
@keyframes leaf-blow {
0% {
transform: translate(-100px, 0) rotate(0deg);
opacity: 0;
}
10% {
opacity: 0.8;
}
100% {
transform: translate(450px, 100px) rotate(720deg);
opacity: 0;
}
}
/* 云朵 */
.cloud {
position: absolute;
background: rgba(255, 255, 255, 0.2);
border-radius: 50%;
opacity: 0;
transition: opacity 1s ease;
}
.cloud1 {
width: 100px;
height: 40px;
top: 70px;
left: 40px;
animation: cloud-drift 20s linear infinite;
}
.cloud2 {
width: 70px;
height: 30px;
top: 120px;
right: 50px;
animation: cloud-drift 15s linear infinite reverse;
}
.cloud::before, .cloud::after {
content: '';
position: absolute;
background: inherit;
border-radius: 50%;
}
.cloud::before {
width: 50%;
height: 100%;
top: -50%;
left: 25%;
}
.cloud::after {
width: 30%;
height: 100%;
top: -30%;
left: 10%;
}
@keyframes cloud-drift {
0%, 100% {
transform: translateX(0);
}
50% {
transform: translateX(50px);
}
}
/* 天气特定样式 */
body.sunny {
background: linear-gradient(to bottom, #1a1a2e, #0f3460);
}
body.rainy {
background: linear-gradient(to bottom, #1a1a2e, #2c3e50);
}
body.snowy {
background: linear-gradient(to bottom, #1a1a2e, #2a3c4d);
}
body.windy {
background: linear-gradient(to bottom, #1a1a2e, #3a4750);
}
/* 响应式设计 */
@media (max-width: 400px) {
.weather-card {
width: 300px;
height: 450px;
padding: 20px;
}
.temperature {
font-size: 3rem;
}
.weather-status {
font-size: 1.2rem;
}
.weather-btn {
width: 50px;
height: 50px;
}
}
</style>
</head>
<body class="sunny">
<div class="weather-card">
<div class="card-header">
<div class="location">北京市</div>
<div class="date">2025年7月6日</div>
</div>
<div class="weather-main">
<div class="weather-icon">
<!-- 动态添加图标 -->
</div>
<div class="temperature">25°C</div>
<div class="weather-status">晴天</div>
</div>
<div class="weather-details">
<div class="detail">
<div class="detail-label">湿度</div>
<div class="detail-value">45%</div>
</div>
<div class="detail">
<div class="detail-label">风速</div>
<div class="detail-value">3 km/h</div>
</div>
<div class="detail">
<div class="detail-label">气压</div>
<div class="detail-value">1013 hPa</div>
</div>
</div>
<div class="weather-switcher">
<button class="weather-btn active" data-weather="sunny">☀️</button>
<button class="weather-btn" data-weather="rainy">🌧️</button>
<button class="weather-btn" data-weather="snowy">❄️</button>
<button class="weather-btn" data-weather="windy">🌬️</button>
</div>
</div>
<div class="animation-container">
<!-- 晴天元素 -->
<div class="sun"></div>
<div class="sun-ray"></div>
<!-- 云朵元素 -->
<div class="cloud cloud1"></div>
<div class="cloud cloud2"></div>
<!-- 其他天气元素由JS动态添加 -->
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
// 获取DOM元素
const body = document.body;
const weatherBtns = document.querySelectorAll('.weather-btn');
const temperatureDisplay = document.querySelector('.temperature');
const weatherStatusDisplay = document.querySelector('.weather-status');
const weatherIcon = document.querySelector('.weather-icon');
const animationContainer = document.querySelector('.animation-container');
const sun = document.querySelector('.sun');
const sunRay = document.querySelector('.sun-ray');
const clouds = document.querySelectorAll('.cloud');
// 天气数据
const weatherData = {
sunny: {
temp: '25°C',
status: '晴天',
humidity: '45%',
windSpeed: '3 km/h',
pressure: '1013 hPa'
},
rainy: {
temp: '18°C',
status: '雨天',
humidity: '85%',
windSpeed: '5 km/h',
pressure: '1008 hPa'
},
snowy: {
temp: '-2°C',
status: '下雪',
humidity: '90%',
windSpeed: '4 km/h',
pressure: '1020 hPa'
},
windy: {
temp: '22°C',
status: '刮风',
humidity: '60%',
windSpeed: '15 km/h',
pressure: '1005 hPa'
}
};
// 清除所有天气特效
function clearWeatherEffects() {
// 移除雨滴
document.querySelectorAll('.rain-drop').forEach(drop => drop.remove());
// 移除雪花
document.querySelectorAll('.snowflake').forEach(flake => flake.remove());
// 移除树叶
document.querySelectorAll('.leaf').forEach(leaf => leaf.remove());
// 隐藏太阳和太阳光
sun.style.opacity = '0';
sunRay.style.opacity = '0';
// 隐藏云朵
clouds.forEach(cloud => cloud.style.opacity = '0');
}
// 创建雨滴元素
function createRaindrops() {
for (let i = 0; i < 50; i++) {
const drop = document.createElement('div');
drop.classList.add('rain-drop');
drop.style.left = `${Math.random() * 100}%`;
drop.style.animationDuration = `${0.8 + Math.random() * 1.5}s`;
drop.style.animationDelay = `${Math.random() * 2}s`;
animationContainer.appendChild(drop);
}
}
// 创建雪花元素
function createSnowflakes() {
for (let i = 0; i < 40; i++) {
const flake = document.createElement('div');
flake.classList.add('snowflake');
flake.style.left = `${Math.random() * 100}%`;
flake.style.animationDuration = `${5 + Math.random() * 10}s`;
flake.style.animationDelay = `${Math.random() * 5}s`;
flake.style.opacity = `${0.4 + Math.random() * 0.6}`;
flake.style.width = `${3 + Math.random() * 5}px`;
flake.style.height = flake.style.width;
animationContainer.appendChild(flake);
}
}
// 创建树叶/风效果
function createWindEffects() {
// 添加飘动的树叶
for (let i = 0; i < 20; i++) {
const leaf = document.createElement('div');
leaf.classList.add('leaf');
leaf.style.top = `${Math.random() * 100}%`;
leaf.style.animationDuration = `${5 + Math.random() * 10}s`;
leaf.style.animationDelay = `${Math.random() * 5}s`;
leaf.style.transform = `rotate(${Math.random() * 360}deg)`;
// 随机绿色调
const greenHue = 100 + Math.floor(Math.random() * 30);
leaf.style.backgroundColor = `hsla(${greenHue}, 70%, 40%, 0.7)`;
// 随机形状
leaf.style.borderRadius = `${30 + Math.random() * 40}% ${60 + Math.random() * 40}% ${30 + Math.random() * 40}% ${60 + Math.random() * 40}% / ${30 + Math.random() * 40}% ${30 + Math.random() * 40}% ${60 + Math.random() * 40}% ${60 + Math.random() * 40}%`;
animationContainer.appendChild(leaf);
}
// 显示云朵并增强动画
clouds.forEach(cloud => {
cloud.style.opacity = '0.7';
cloud.style.animationDuration = '10s';
});
}
// 显示晴天效果
function showSunnyEffects() {
sun.style.opacity = '1';
sunRay.style.opacity = '0.7';
// 显示一些淡淡的云
clouds.forEach(cloud => {
cloud.style.opacity = '0.3';
});
}
// 更新天气详细信息
function updateWeatherDetails(weather) {
document.querySelector('.detail:nth-child(1) .detail-value').textContent = weatherData[weather].humidity;
document.querySelector('.detail:nth-child(2) .detail-value').textContent = weatherData[weather].windSpeed;
document.querySelector('.detail:nth-child(3) .detail-value').textContent = weatherData[weather].pressure;
}
// 设置天气状态
function setWeather(weather) {
// 清除当前效果
clearWeatherEffects();
// 更新UI
temperatureDisplay.textContent = weatherData[weather].temp;
weatherStatusDisplay.textContent = weatherData[weather].status;
updateWeatherDetails(weather);
// 设置body类名以更改背景
body.className = weather;
// 根据天气类型应用特效
switch(weather) {
case 'sunny':
showSunnyEffects();
break;
case 'rainy':
createRaindrops();
// 显示云朵
clouds.forEach(cloud => {
cloud.style.opacity = '0.7';
});
break;
case 'snowy':
createSnowflakes();
// 显示云朵
clouds.forEach(cloud => {
cloud.style.opacity = '0.7';
});
break;
case 'windy':
createWindEffects();
break;
}
// 更新按钮激活状态
weatherBtns.forEach(btn => {
btn.classList.remove('active');
if (btn.dataset.weather === weather) {
btn.classList.add('active');
}
});
}
// 设置按钮点击事件
weatherBtns.forEach(btn => {
btn.addEventListener('click', function() {
setWeather(this.dataset.weather);
});
});
// 初始化天气状态
setWeather('sunny');
// 更新日期
const today = new Date();
const options = { year: 'numeric', month: 'long', day: 'numeric' };
document.querySelector('.date').textContent = today.toLocaleDateString('zh-CN', options);
});
</script>
</body>
</html>
您想要我解释或详细说明这段代码吗?