html
<!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 {
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
position: relative;
}
.weather-card {
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 0 rgba(31, 38, 135, 0.37);
position: relative;
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.05) 100%);
z-index: 0;
}
.weather-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
.location {
font-size: 1.2rem;
font-weight: 600;
}
.date {
font-size: 0.9rem;
opacity: 0.8;
}
.weather-main {
position: relative;
z-index: 1;
text-align: center;
margin-bottom: 20px;
}
.weather-icon {
font-size: 5rem;
margin-bottom: 10px;
}
.temperature {
font-size: 3rem;
font-weight: 300;
margin-bottom: 5px;
}
.weather-description {
font-size: 1.1rem;
opacity: 0.9;
margin-bottom: 15px;
}
.weather-details {
display: flex;
justify-content: space-around;
margin-bottom: 20px;
}
.detail-item {
text-align: center;
}
.detail-icon {
font-size: 1.5rem;
margin-bottom: 5px;
}
.detail-value {
font-size: 1.1rem;
font-weight: 500;
}
.detail-label {
font-size: 0.8rem;
opacity: 0.8;
}
.weather-controls {
display: flex;
justify-content: space-between;
margin-top: 20px;
}
.control-btn {
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 10px;
color: white;
padding: 8px 15px;
font-size: 0.9rem;
cursor: pointer;
transition: all 0.3s ease;
backdrop-filter: blur(5px);
}
.control-btn:hover {
background: rgba(255, 255, 255, 0.2);
border-color: rgba(255, 255, 255, 0.2);
}
/* 雨天动画 */
.rain {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 0;
}
.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: rgba(255, 255, 255, 0.8);
border-radius: 50%;
animation: fall linear infinite;
}
/* 风天动画 */
.wind {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 0;
}
.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.1) 0%, transparent 70%);
transform: translate(-50%, -50%);
z-index: 0;
opacity: 0.5;
}
.sun-rays::before {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 10px;
height: 10px;
background: rgba(255, 255, 0, 0.8);
border-radius: 50%;
transform: translate(-50%, -50%);
}
/* 动画 */
@keyframes fall {
0% {
transform: translateY(-100px);
}
100% {
transform: translateY(100px);
}
}
@keyframes float {
0% {
transform: translate(0, 0);
}
100% {
transform: translate(50px, 50px);
}
}
/* 不同天气状态的背景 */
.sunny {
background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
}
.rainy {
background: linear-gradient(135deg, rgba(0, 150, 255, 0.1) 0%, rgba(0, 100, 200, 0.1) 100%);
}
.snowy {
background: linear-gradient(135deg, rgba(200, 220, 255, 0.1) 0%, rgba(150, 180, 255, 0.1) 100%);
}
.windy {
background: linear-gradient(135deg, rgba(200, 200, 255, 0.1) 0%, rgba(150, 150, 255, 0.1) 100%);
}
/* 晴天特效 */
.sunny .sun-rays {
animation: sunPulse 4s ease-in-out infinite;
}
@keyframes sunPulse {
0% {
transform: translate(-50%, -50%) scale(1);
opacity: 0.5;
}
50% {
transform: translate(-50%, -50%) scale(1.1);
opacity: 0.7;
}
100% {
transform: translate(-50%, -50%) scale(1);
opacity: 0.5;
}
}
/* 晴天云朵 */
.sunny .cloud {
position: absolute;
width: 50px;
height: 30px;
background: rgba(255, 255, 255, 0.2);
border-radius: 50px;
top: 30%;
left: 20%;
animation: cloudFloat 20s linear infinite;
}
.sunny .cloud::before, .sunny .cloud::after {
content: '';
position: absolute;
width: 30px;
height: 20px;
background: rgba(255, 255, 255, 0.2);
border-radius: 50px;
}
.sunny .cloud::before {
left: -20px;
}
.sunny .cloud::after {
right: -20px;
}
@keyframes cloudFloat {
0% {
transform: translateX(0);
}
100% {
transform: translateX(100px);
}
}
</style>
</head>
<body>
<div class="weather-card sunny">
<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="weather-description">晴天</div>
</div>
<div class="weather-details">
<div class="detail-item">
<div class="detail-icon">💧</div>
<div class="detail-value">45%</div>
<div class="detail-label">湿度</div>
</div>
<div class="detail-item">
<div class="detail-icon">💨</div>
<div class="detail-value">12 km/h</div>
<div class="detail-label">风速</div>
</div>
<div class="detail-item">
<div class="detail-icon">🌡️</div>
<div class="detail-value">22°C</div>
<div class="detail-label">体感</div>
</div>
</div>
<div class="weather-controls">
<button class="control-btn" id="prev-btn">上一个</button>
<button class="control-btn" id="next-btn">下一个</button>
</div>
<!-- 晴天特效 -->
<div class="sun-rays"></div>
<div class="cloud"></div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const weatherCard = document.querySelector('.weather-card');
const prevBtn = document.getElementById('prev-btn');
const nextBtn = document.getElementById('next-btn');
const weatherTypes = [
{ type: 'sunny', icon: '☀️', temp: '25°C', desc: '晴天', humidity: '45%', wind: '12 km/h', feelsLike: '22°C' },
{ type: 'rainy', icon: '🌧️', temp: '18°C', desc: '雨天', humidity: '85%', wind: '8 km/h', feelsLike: '16°C' },
{ type: 'snowy', icon: '❄️', temp: '-3°C', desc: '下雪', humidity: '70%', wind: '15 km/h', feelsLike: '-6°C' },
{ type: 'windy', icon: '🌪️', temp: '12°C', desc: '刮风', humidity: '60%', wind: '25 km/h', feelsLike: '8°C' }
];
let currentIndex = 0;
function updateWeather(type) {
// 移除所有动画元素
const rainContainer = document.querySelector('.rain');
if (rainContainer) rainContainer.remove();
const snowContainer = document.querySelector('.snow');
if (snowContainer) snowContainer.remove();
const windContainer = document.querySelector('.wind');
if (windContainer) windContainer.remove();
const cloud = document.querySelector('.cloud');
if (cloud) cloud.remove();
// 更新卡片类名
weatherCard.className = 'weather-card ' + type.type;
// 更新天气信息
document.querySelector('.weather-icon').textContent = type.icon;
document.querySelector('.temperature').textContent = type.temp;
document.querySelector('.weather-description').textContent = type.desc;
document.querySelectorAll('.detail-value')[0].textContent = type.humidity;
document.querySelectorAll('.detail-value')[1].textContent = type.wind;
document.querySelectorAll('.detail-value')[2].textContent = type.feelsLike;
// 添加对应的动画效果
if (type.type === 'rainy') {
createRain();
} else if (type.type === 'snowy') {
createSnow();
} else if (type.type === 'windy') {
createWind();
} else if (type.type === 'sunny') {
createSun();
}
}
function createRain() {
const rainContainer = document.createElement('div');
rainContainer.className = 'rain';
weatherCard.appendChild(rainContainer);
for (let i = 0; i < 100; i++) {
const drop = document.createElement('div');
drop.className = 'rain-drop';
// 随机位置
drop.style.left = Math.random() * 100 + 'px';
drop.style.animationDuration = Math.random() * 2 + 1 + 's';
drop.style.animationDelay = Math.random() * 2 + 's';
rainContainer.appendChild(drop);
}
}
function createSnow() {
const snowContainer = document.createElement('div');
snowContainer.className = 'snow';
weatherCard.appendChild(snowContainer);
for (let i = 0; i < 100; i++) {
const snowflake = document.createElement('div');
snowflake.className = 'snowflake';
// 随机位置
snowflake.style.left = Math.random() * 100 + 'px';
snowflake.style.top = Math.random() * -100 + 'px';
snowflake.style.animationDuration = Math.random() * 5 + 5 + 's';
snowflake.style.animationDelay = Math.random() * 2 + 's';
// 随机大小
const size = Math.random() * 2 + 1;
snowflake.style.width = size + 'px';
snowflake.style.height = size + 'px';
snowContainer.appendChild(snowflake);
}
}
function createWind() {
const windContainer = document.createElement('div');
windContainer.className = 'wind';
weatherCard.appendChild(windContainer);
for (let i = 0; i < 20; i++) {
const leaf = document.createElement('div');
leaf.className = 'wind-leaf';
// 随机位置
leaf.style.left = Math.random() * 100 + 'px';
leaf.style.top = Math.random() * 100 + 'px';
leaf.style.animationDuration = Math.random() * 5 + 5 + 's';
leaf.style.animationDelay = Math.random() * 2 + 's';
// 随机形状
const shape = Math.random();
if (shape < 0.3) {
leaf.style.borderRadius = '50%';
} else if (shape < 0.6) {
leaf.style.borderRadius = '50% 50% 0 0';
} else {
leaf.style.borderRadius = '50% 0 50% 0';
}
windContainer.appendChild(leaf);
}
}
function createSun() {
const cloud = document.createElement('div');
cloud.className = 'cloud';
cloud.style.left = Math.random() * 50 + 'px';
cloud.style.top = Math.random() * 20 + 30 + '%';
cloud.style.animationDuration = Math.random() * 20 + 20 + 's';
weatherCard.appendChild(cloud);
}
prevBtn.addEventListener('click', function() {
currentIndex = (currentIndex - 1 + weatherTypes.length) % weatherTypes.length;
updateWeather(weatherTypes[currentIndex]);
});
nextBtn.addEventListener('click', function() {
currentIndex = (currentIndex + 1) % weatherTypes.length;
updateWeather(weatherTypes[currentIndex]);
});
// 初始化
updateWeather(weatherTypes[currentIndex]);
});
</script>
</body>
</html>