html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>高质量天气卡片</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap');
/* Reset & base */
* {
margin: 0; padding: 0; box-sizing: border-box;
}
body, html {
height: 100%;
background: #0a0a0f;
font-family: 'Montserrat', sans-serif;
display: flex;
justify-content: center;
align-items: center;
color: #eee;
user-select: none;
}
/* Card container with frosted glass effect */
.weather-card {
position: relative;
width: 320px;
height: 440px;
border-radius: 24px;
background: rgba(20, 20, 30, 0.4);
box-shadow:
0 8px 32px 0 rgba(0,0,0,0.7),
inset 0 0 60px 10px rgba(255,255,255,0.05);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
border: 1px solid rgba(255,255,255,0.1);
overflow: hidden;
display: flex;
flex-direction: column;
padding: 24px 32px;
}
/* Header with city and date */
.header {
text-align: center;
margin-bottom: 16px;
}
.header h1 {
font-weight: 700;
font-size: 2.2rem;
letter-spacing: 1.2px;
margin-bottom: 4px;
}
.header p {
font-weight: 400;
font-size: 0.9rem;
color: #bbb;
}
/* Weather icon container */
.weather-icon {
position: relative;
width: 160px;
height: 160px;
margin: 0 auto 24px;
}
/* Temperature */
.temperature {
font-size: 5rem;
font-weight: 700;
text-align: center;
letter-spacing: 2px;
margin-bottom: 8px;
text-shadow: 0 0 10px rgba(255,255,255,0.3);
}
/* Weather description */
.description {
font-size: 1.3rem;
font-weight: 600;
text-align: center;
color: #ddd;
margin-bottom: 24px;
text-transform: capitalize;
letter-spacing: 1.1px;
}
/* Buttons container */
.buttons {
display: flex;
justify-content: center;
gap: 14px;
}
.buttons button {
background: rgba(255,255,255,0.1);
border: none;
border-radius: 14px;
padding: 10px 18px;
color: #eee;
font-weight: 600;
font-size: 0.9rem;
cursor: pointer;
transition: background 0.3s ease, color 0.3s ease;
box-shadow: 0 0 8px rgba(255,255,255,0.1);
user-select: none;
}
.buttons button:hover,
.buttons button.active {
background: #5a8dee;
color: #fff;
box-shadow: 0 0 15px #5a8dee;
}
/* Animations container */
.animation-layer {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
pointer-events: none;
overflow: hidden;
border-radius: 24px;
z-index: 0;
}
/* Common shapes */
.sun {
position: relative;
width: 120px;
height: 120px;
margin: 0 auto;
border-radius: 50%;
background: radial-gradient(circle at center, #ffd93b 60%, #f0b90b 100%);
box-shadow:
0 0 30px 10px #ffd93b88,
0 0 60px 20px #ffd93b55;
animation: sunRotate 20s linear infinite;
}
@keyframes sunRotate {
0% { transform: rotate(0deg);}
100% { transform: rotate(360deg);}
}
.sun-rays {
position: absolute;
top: 50%; left: 50%;
width: 160px;
height: 160px;
margin-left: -80px;
margin-top: -80px;
border-radius: 50%;
box-shadow:
0 -70px 20px 5px #ffd93b88,
49px -49px 20px 5px #ffd93b88,
70px 0 20px 5px #ffd93b88,
49px 49px 20px 5px #ffd93b88,
0 70px 20px 5px #ffd93b88,
-49px 49px 20px 5px #ffd93b88,
-70px 0 20px 5px #ffd93b88,
-49px -49px 20px 5px #ffd93b88;
animation: raysPulse 3s ease-in-out infinite;
}
@keyframes raysPulse {
0%, 100% { opacity: 0.7; }
50% { opacity: 1; }
}
/* Rain drops */
.raindrop {
position: absolute;
bottom: 100%;
width: 4px;
height: 18px;
background: linear-gradient(180deg, #a3c4f3 0%, #3a6fcf 100%);
border-radius: 2px 2px 6px 6px;
opacity: 0.7;
animation: fallRain linear infinite;
}
@keyframes fallRain {
0% {
transform: translateY(0) translateX(0) rotate(0deg);
opacity: 0.7;
}
100% {
transform: translateY(420px) translateX(10px) rotate(15deg);
opacity: 0;
}
}
/* Snow flakes */
.snowflake {
position: absolute;
top: -20px;
width: 12px;
height: 12px;
background: radial-gradient(circle, #fff 60%, transparent 100%);
border-radius: 50%;
opacity: 0.9;
filter: drop-shadow(0 0 2px #fff);
animation: fallSnow linear infinite;
}
@keyframes fallSnow {
0% {
transform: translateY(0) translateX(0) rotate(0deg);
opacity: 0.9;
}
100% {
transform: translateY(440px) translateX(20px) rotate(360deg);
opacity: 0;
}
}
/* Wind leaves */
.leaf {
position: absolute;
width: 20px;
height: 12px;
background: linear-gradient(135deg, #a3d977 0%, #5a8e3d 100%);
border-radius: 10px 10px 10px 10px / 50% 50% 50% 50%;
filter: drop-shadow(0 0 2px #4a6e1a);
opacity: 0.8;
animation: flyLeaf linear infinite;
}
@keyframes flyLeaf {
0% {
transform: translateX(-40px) translateY(0) rotate(0deg) scale(1);
opacity: 0.8;
}
50% {
opacity: 1;
transform: translateX(160px) translateY(80px) rotate(180deg) scale(1.1);
}
100% {
transform: translateX(320px) translateY(0) rotate(360deg) scale(1);
opacity: 0;
}
}
/* Cloud */
.cloud {
position: relative;
width: 140px;
height: 80px;
background: rgba(255,255,255,0.15);
border-radius: 50px;
box-shadow:
40px 10px 30px rgba(255,255,255,0.15),
70px 20px 30px rgba(255,255,255,0.15);
animation: cloudFloat 8s ease-in-out infinite;
}
@keyframes cloudFloat {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-10px); }
}
/* Background colors for each weather */
.weather-card.sunny {
background: rgba(30, 30, 50, 0.5);
box-shadow:
0 8px 32px 0 rgba(255, 200, 50, 0.6),
inset 0 0 60px 10px rgba(255, 255, 150, 0.1);
}
.weather-card.rainy {
background: rgba(10, 20, 40, 0.6);
box-shadow:
0 8px 32px 0 rgba(50, 100, 200, 0.7),
inset 0 0 60px 10px rgba(50, 100, 200, 0.15);
}
.weather-card.snowy {
background: rgba(20, 30, 50, 0.7);
box-shadow:
0 8px 32px 0 rgba(220, 240, 255, 0.7),
inset 0 0 60px 10px rgba(220, 240, 255, 0.15);
}
.weather-card.windy {
background: rgba(15, 15, 25, 0.7);
box-shadow:
0 8px 32px 0 rgba(150, 150, 180, 0.7),
inset 0 0 60px 10px rgba(150, 150, 180, 0.15);
}
/* Animations container z-index */
.animation-layer {
z-index: 0;
}
.weather-icon, .temperature, .description, .buttons, .header {
position: relative;
z-index: 1;
}
</style>
</head>
<body>
<div class="weather-card sunny" id="weatherCard" aria-label="天气卡片">
<div class="animation-layer" id="animationLayer"></div>
<div class="header">
<h1 id="cityName">北京</h1>
<p id="dateText"></p>
</div>
<div class="weather-icon" id="weatherIcon" aria-hidden="true"></div>
<div class="temperature" id="temperature">26°C</div>
<div class="description" id="description">晴天</div>
<div class="buttons" role="group" aria-label="切换天气状态">
<button type="button" data-weather="sunny" class="active" aria-pressed="true">晴天</button>
<button type="button" data-weather="rainy" aria-pressed="false">雨天</button>
<button type="button" data-weather="snowy" aria-pressed="false">下雪</button>
<button type="button" data-weather="windy" aria-pressed="false">刮风</button>
</div>
</div>
<script>
(() => {
const weatherCard = document.getElementById('weatherCard');
const animationLayer = document.getElementById('animationLayer');
const weatherIcon = document.getElementById('weatherIcon');
const temperatureEl = document.getElementById('temperature');
const descriptionEl = document.getElementById('description');
const buttons = document.querySelectorAll('.buttons button');
const dateText = document.getElementById('dateText');
// Update date text
function updateDate() {
const now = new Date();
const options = { weekday: 'long', month: 'long', day: 'numeric' };
dateText.textContent = now.toLocaleDateString('zh-CN', options);
}
updateDate();
// Weather data for demo
const weatherData = {
sunny: {
temp: 26,
desc: '晴天',
icon: createSunnyIcon,
bgClass: 'sunny',
},
rainy: {
temp: 18,
desc: '雨天',
icon: createRainyIcon,
bgClass: 'rainy',
},
snowy: {
temp: -2,
desc: '下雪',
icon: createSnowyIcon,
bgClass: 'snowy',
},
windy: {
temp: 15,
desc: '刮风',
icon: createWindyIcon,
bgClass: 'windy',
}
};
// Clear animation layer and icon
function clearAnimations() {
animationLayer.innerHTML = '';
weatherIcon.innerHTML = '';
}
// Create Sunny Icon + animation
function createSunnyIcon() {
// Sun core
const sun = document.createElement('div');
sun.className = 'sun';
weatherIcon.appendChild(sun);
// Sun rays
const rays = document.createElement('div');
rays.className = 'sun-rays';
weatherIcon.appendChild(rays);
}
// Create Rainy Icon + animation
function createRainyIcon() {
// Cloud
const cloud = document.createElement('div');
cloud.className = 'cloud';
weatherIcon.appendChild(cloud);
// Create raindrops
for (let i = 0; i < 15; i++) {
const drop = document.createElement('div');
drop.className = 'raindrop';
drop.style.left = `${Math.random() * 140}px`;
drop.style.animationDuration = `${0.8 + Math.random() * 0.7}s`;
drop.style.animationDelay = `${Math.random() * 2}s`;
animationLayer.appendChild(drop);
}
}
// Create Snowy Icon + animation
function createSnowyIcon() {
// Cloud
const cloud = document.createElement('div');
cloud.className = 'cloud';
weatherIcon.appendChild(cloud);
// Create snowflakes
for (let i = 0; i < 20; i++) {
const flake = document.createElement('div');
flake.className = 'snowflake';
flake.style.left = `${Math.random() * 300}px`;
flake.style.animationDuration = `${4 + Math.random() * 4}s`;
flake.style.animationDelay = `${Math.random() * 5}s`;
flake.style.width = `${8 + Math.random() * 8}px`;
flake.style.height = flake.style.width;
animationLayer.appendChild(flake);
}
}
// Create Windy Icon + animation
function createWindyIcon() {
// Cloud
const cloud = document.createElement('div');
cloud.className = 'cloud';
weatherIcon.appendChild(cloud);
// Create leaves flying
for (let i = 0; i < 8; i++) {
const leaf = document.createElement('div');
leaf.className = 'leaf';
leaf.style.top = `${20 + Math.random() * 200}px`;
leaf.style.animationDuration = `${4 + Math.random() * 4}s`;
leaf.style.animationDelay = `${Math.random() * 4}s`;
leaf.style.left = `-40px`;
animationLayer.appendChild(leaf);
}
}
// Switch weather
function switchWeather(weather) {
if (!weatherData[weather]) return;
clearAnimations();
// Update card background
weatherCard.className = `weather-card ${weatherData[weather].bgClass}`;
// Update temperature and description
temperatureEl.textContent = `${weatherData[weather].temp}°C`;
descriptionEl.textContent = weatherData[weather].desc;
// Create icon and animations
weatherData[weather].icon();
// Update buttons active state and aria-pressed
buttons.forEach(btn => {
if (btn.dataset.weather === weather) {
btn.classList.add('active');
btn.setAttribute('aria-pressed', 'true');
} else {
btn.classList.remove('active');
btn.setAttribute('aria-pressed', 'false');
}
});
}
// Initial weather
switchWeather('sunny');
// Button event listeners
buttons.forEach(btn => {
btn.addEventListener('click', () => {
switchWeather(btn.dataset.weather);
});
});
})();
</script>
</body>
</html>