高质量天气卡片
以下是一个现代风格的天气卡片,包含四种天气状态(晴天、雨天、下雪、刮风)及对应的动画效果,使用纯HTML/CSS/JS实现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modern Weather Card</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
overflow: hidden;
}
.weather-card {
position: relative;
width: 350px;
height: 500px;
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 20px;
border: 1px solid rgba(255, 255, 255, 0.1);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
overflow: hidden;
display: flex;
flex-direction: column;
justify-content: space-between;
padding: 20px;
color: white;
transition: all 0.5s ease;
}
.weather-card::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0) 50%);
z-index: 0;
}
.weather-header {
display: flex;
justify-content: space-between;
align-items: center;
padding-bottom: 20px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.location {
font-size: 1.2rem;
font-weight: 300;
}
.date {
font-size: 0.9rem;
opacity: 0.8;
}
.weather-main {
position: relative;
flex-grow: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 1;
}
.weather-icon {
font-size: 5rem;
margin-bottom: 20px;
transition: all 0.5s ease;
}
.temperature {
font-size: 3.5rem;
font-weight: 300;
margin-bottom: 10px;
}
.description {
font-size: 1.1rem;
opacity: 0.9;
margin-bottom: 20px;
text-align: center;
}
.weather-details {
display: flex;
justify-content: space-around;
width: 100%;
padding-top: 20px;
border-top: 1px solid rgba(255, 255, 255, 0.1);
}
.detail-item {
text-align: center;
}
.detail-label {
font-size: 0.8rem;
opacity: 0.7;
}
.detail-value {
font-size: 1.2rem;
font-weight: 300;
}
.weather-controls {
display: flex;
justify-content: center;
gap: 10px;
margin-top: 20px;
}
.weather-btn {
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.1);
color: white;
border-radius: 50px;
padding: 8px 15px;
font-size: 0.9rem;
cursor: pointer;
transition: all 0.3s ease;
backdrop-filter: blur(5px);
}
.weather-btn:hover {
background: rgba(255, 255, 255, 0.2);
border-color: rgba(255, 255, 255, 0.2);
}
.weather-btn.active {
background: white;
color: #1a1a2e;
font-weight: 600;
}
/* 雨天动画 */
.rain {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 0;
pointer-events: none;
}
.rain-drop {
position: absolute;
width: 1px;
height: 15px;
background: rgba(255, 255, 255, 0.5);
border-radius: 1px;
animation: fall linear infinite;
}
/* 雪天动画 */
.snowflake {
position: absolute;
width: 3px;
height: 3px;
background: white;
border-radius: 50%;
animation: fall linear infinite;
}
/* 风天动画 */
.wind {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 0;
pointer-events: none;
}
.wind-leaf {
position: absolute;
width: 15px;
height: 15px;
background: rgba(255, 255, 255, 0.3);
border-radius: 50%;
animation: float linear infinite;
}
/* 晴天动画 */
.sun-rays {
position: absolute;
top: 50%;
left: 50%;
width: 200px;
height: 200px;
border-radius: 50%;
background: radial-gradient(circle, rgba(255, 255, 0, 0.3) 0%, transparent 70%);
transform: translate(-50%, -50%);
z-index: 0;
opacity: 0;
transition: opacity 0.5s ease;
}
.sun {
position: absolute;
top: 50%;
left: 50%;
width: 60px;
height: 60px;
background: radial-gradient(circle, #ffcc00 0%, #ff9900 100%);
border-radius: 50%;
transform: translate(-50%, -50%);
z-index: 0;
box-shadow: 0 0 20px 5px rgba(255, 204, 0, 0.5);
}
/* 动画 */
@keyframes fall {
0% {
transform: translateY(-100vh) rotate(0deg);
}
100% {
transform: translateY(100vh) rotate(360deg);
}
}
@keyframes float {
0% {
transform: translate(0, 0) rotate(0deg);
}
100% {
transform: translate(100px, 100px) rotate(360deg);
}
}
/* 天气状态背景 */
.weather-card.sunny {
background: rgba(255, 255, 255, 0.1);
box-shadow: 0 0 30px rgba(255, 204, 0, 0.3);
}
.weather-card.rainy {
background: rgba(0, 150, 255, 0.1);
box-shadow: 0 0 30px rgba(0, 150, 255, 0.3);
}
.weather-card.snowy {
background: rgba(200, 230, 255, 0.1);
box-shadow: 0 0 30px rgba(200, 230, 255, 0.3);
}
.weather-card.windy {
background: rgba(150, 150, 255, 0.1);
box-shadow: 0 0 30px rgba(150, 150, 255, 0.3);
}
</style>
</head>
<body>
<div class="weather-card" id="weatherCard">
<div class="weather-header">
<div class="location">北京</div>
<div class="date">2023年10月15日</div>
</div>
<div class="weather-main">
<div class="weather-icon">☀️</div>
<div class="temperature">25°C</div>
<div class="description">晴天,阳光明媚</div>
</div>
<div class="weather-details">
<div class="detail-item">
<div class="detail-label">湿度</div>
<div class="detail-value">65%</div>
</div>
<div class="detail-item">
<div class="detail-label">风速</div>
<div class="detail-value">12 km/h</div>
</div>
<div class="detail-item">
<div class="detail-label">气压</div>
<div class="detail-value">1012 hPa</div>
</div>
</div>
<div class="weather-controls">
<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 class="rain" id="rain"></div>
<!-- 雪天动画 -->
<div class="snow" id="snow"></div>
<!-- 风天动画 -->
<div class="wind" id="wind"></div>
<!-- 晴天动画 -->
<div class="sun-rays" id="sunRays"></div>
<div class="sun" id="sun"></div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const weatherCard = document.getElementById('weatherCard');
const weatherBtns = document.querySelectorAll('.weather-btn');
const rain = document.getElementById('rain');
const snow = document.getElementById('snow');
const wind = document.getElementById('wind');
const sunRays = document.getElementById('sunRays');
const sun = document.getElementById('sun');
// 初始化天气
updateWeather('sunny');
// 切换天气
weatherBtns.forEach(btn => {
btn.addEventListener('click', function() {
const weatherType = this.getAttribute('data-weather');
updateWeather(weatherType);
// 更新按钮活动状态
weatherBtns.forEach(b => b.classList.remove('active'));
this.classList.add('active');
});
});
function updateWeather(weatherType) {
// 更新卡片类名
weatherCard.classList.remove('sunny', 'rainy', 'snowy', 'windy');
weatherCard.classList.add(weatherType);
// 更新天气图标和描述
const weatherData = {
sunny: {
icon: '☀️',
temp: '25°C',
desc: '晴天,阳光明媚',
humidity: '65%',
wind: '12 km/h',
pressure: '1012 hPa'
},
rainy: {
icon: '🌧️',
temp: '18°C',
desc: '雨天,带伞出门',
humidity: '85%',
wind: '8 km/h',
pressure: '1010 hPa'
},
snowy: {
icon: '❄️',
temp: '5°C',
desc: '下雪,注意保暖',
humidity: '75%',
wind: '15 km/h',
pressure: '1015 hPa'
},
windy: {
icon: '🌪️',
temp: '15°C',
desc: '刮风,注意安全',
humidity: '55%',
wind: '25 km/h',
pressure: '1008 hPa'
}
};
const data = weatherData[weatherType];
document.querySelector('.weather-icon').textContent = data.icon;
document.querySelector('.temperature').textContent = data.temp;
document.querySelector('.description').textContent = data.desc;
document.querySelectorAll('.detail-value')[0].textContent = data.humidity;
document.querySelectorAll('.detail-value')[1].textContent = data.wind;
document.querySelectorAll('.detail-value')[2].textContent = data.pressure;
// 更新动画
updateAnimations(weatherType);
}
function updateAnimations(weatherType) {
// 清除所有动画
rain.innerHTML = '';
snow.innerHTML = '';
wind.innerHTML = '';
// 晴天动画
if (weatherType === 'sunny') {
sunRays.style.opacity = '1';
sun.style.display = 'block';
// 创建阳光射线
for (let i = 0; i < 10; i++) {
const ray = document.createElement('div');
ray.style.position = 'absolute';
ray.style.width = '2px';
ray.style.height = '100px';
ray.style.background = 'rgba(255, 255, 0, 0.2)';
ray.style.transform = `rotate(${i * 36}deg)`;
ray.style.transformOrigin = 'top center';
ray.style.top = '0';
ray.style.left = '50%';
sunRays.appendChild(ray);
}
} else {
sunRays.style.opacity = '0';
sun.style.display = 'none';
}
// 雨天动画
if (weatherType === 'rainy') {
for (let i = 0; i < 100; i++) {
const drop = document.createElement('div');
drop.className = 'rain-drop';
drop.style.left = `${Math.random() * 100}%`;
drop.style.animationDuration = `${Math.random() * 3 + 2}s`;
drop.style.animationDelay = `${Math.random() * 2}s`;
rain.appendChild(drop);
}
}
// 雪天动画
if (weatherType === 'snowy') {
for (let i = 0; i < 100; i++) {
const flake = document.createElement('div');
flake.className = 'snowflake';
flake.style.left = `${Math.random() * 100}%`;
flake.style.animationDuration = `${Math.random() * 5 + 3}s`;
flake.style.animationDelay = `${Math.random() * 2}s`;
flake.style.width = `${Math.random() * 3 + 2}px`;
flake.style.height = `${Math.random() * 3 + 2}px`;
flake.style.opacity = `${Math.random() * 0.5 + 0.3}`;
snow.appendChild(flake);
}
}
// 风天动画
if (weatherType === 'windy') {
for (let i = 0; i < 20; i++) {
const leaf = document.createElement('div');
leaf.className = 'wind-leaf';
leaf.style.left = `${Math.random() * 100}%`;
leaf.style.top = `${Math.random() * 100}%`;
leaf.style.animationDuration = `${Math.random() * 5 + 3}s`;
leaf.style.animationDelay = `${Math.random() * 2}s`;
leaf.style.borderRadius = `${Math.random() * 50}%`;
wind.appendChild(leaf);
}
}
}
});
</script>
</body>
</html>
功能说明
-
现代设计风格:
- 使用毛玻璃效果(backdrop-filter)和模糊背景
- 深色背景搭配高对比度元素
- 圆角设计和渐变效果
- 微妙的动画过渡
-
四种天气状态:
- 晴天:阳光射线和太阳动画
- 雨天:雨滴下落动画
- 下雪:雪花飘落动画
- 刮风:树叶飘动动画
-
交互功能:
- 顶部按钮可切换不同天气状态
- 每种天气状态有对应的背景颜色和动画
- 详细天气信息随天气变化而变化
-
细节优化:
- 所有动画都是纯CSS/JS实现
- 响应式设计,适应不同屏幕尺寸
- 过渡动画使切换更流畅
您可以将此代码保存为HTML文件,直接在浏览器中打开查看效果。