好的,我们来创建一个具有视觉吸引力的横版天气页面。这个页面将包含一个城市选择器和一个根据所选城市实时(模拟)更新的天气卡片。卡片将根据天气状况(晴天、大风、暴雨、暴雪)呈现不同的华丽外观和动态效果。
我们将使用 HTML, CSS (包括 Grid/Flexbox, 动画, 阴影, 滤镜) 和一点点 JavaScript 来实现。
HTML (index.html):
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>沉浸式天气卡片</title>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;700&family=Pacifico&display=swap" rel="stylesheet">
</head>
<body>
<div class="weather-app-container">
<header>
<h1>✨ 动态天气视觉器 ✨</h1>
<div class="select-wrapper">
<label for="city-select">选择城市:</label>
<select id="city-select" name="city">
<option value="">--请选择--</option>
<option value="beijing">北京</option>
<option value="shanghai">上海</option>
<option value="guangzhou">广州</option>
<option value="london">伦敦</option>
<option value="tokyo">东京</option>
<option value="newyork">纽约</option>
</select>
</div>
</header>
<main class="weather-cards-container">
<!-- 天气卡片将在这里被 JavaScript 动态添加或更新 -->
<div id="weather-card-main" class="weather-card" style="display: none;">
<div class="card-content">
<div class="icon-container">
<span class="icon"></span>
</div>
<h2 class="city-name"></h2>
<p class="temperature"></p>
<p class="description"></p>
<div class="details">
<span>风速: <span class="wind-speed"></span></span>
<span>湿度: <span class="humidity"></span></span>
</div>
</div>
<div class="card-overlay"></div>
<div class="card-glow"></div>
</div>
<div id="placeholder-card" class="weather-card placeholder">
<div class="card-content">
<p>请先选择一个城市</p>
</div>
</div>
</main>
<footer>
<p>感受不同天气带来的视觉之美</p>
</footer>
</div>
<script src="script.js"></script>
</body>
</html>
CSS (style.css):
/* 全局 & 背景 */
:root {
--sunny-color-start: #FFDA6B;
--sunny-color-end: #FFB347;
--windy-color-start: #A7B6C5;
--windy-color-end: #7E98A9;
--rainy-color-start: #5C7E8A;
--rainy-color-end: #3A5B6C;
--snowy-color-start: #CAD2D9;
--snowy-color-end: #A0B0BF;
--default-text-color: #fff;
--shadow-color: rgba(0, 0, 0, 0.4);
--blur-amount: 8px;
--transition-speed: 0.4s;
}
body {
font-family: 'Montserrat', sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #647DEE 0%, #7F5FEB 100%);
color: var(--default-text-color);
margin: 0;
overflow: hidden; /* 防止页面滚动 */
position: relative;
padding: 20px;
box-sizing: border-box;
}
/* 页面容器 */
.weather-app-container {
width: 90%;
max-width: 1200px;
text-align: center;
}
/* 标题和选择器 */
header h1 {
font-family: 'Pacifico', cursive;
font-size: 3rem;
margin-bottom: 30px;
color: rgba(255, 255, 255, 0.9);
text-shadow: 0 5px 15px var(--shadow-color);
animation: floatText 6s ease-in-out infinite;
}
.select-wrapper {
margin-bottom: 40px;
display: inline-block; /* 让背景包裹内容 */
background: rgba(255, 255, 255, 0.15);
padding: 10px 20px;
border-radius: 25px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(5px); /* 毛玻璃效果 */
}
.select-wrapper label {
margin-right: 10px;
font-weight: bold;
color: rgba(255, 255, 255, 0.8);
}
#city-select {
background: none;
border: none;
color: var(--default-text-color);
font-size: 1.1rem;
padding: 5px 10px;
cursor: pointer;
outline: none;
border-radius: 5px;
transition: background var(--transition-speed) ease;
}
#city-select:hover,
#city-select:focus {
background: rgba(255, 255, 255, 0.3);
}
/* 横向卡片容器 */
.weather-cards-container {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center;
gap: 30px; /* 卡片间距 */
padding: 20px 0;
overflow-x: auto; /* 如果卡片很多,允许横向滚动 */
scrollbar-width: none; /* Firefox */
-ms-overflow-style: none; /* Internet Explorer 10+ */
}
.weather-cards-container::-webkit-scrollbar {
display: none; /* WebKit */
}
/* 天气卡片基础样式 */
.weather-card {
flex: 0 0 300px; /* 固定宽度,不伸缩,不收缩 */
height: 400px;
background: rgba(255, 255, 255, 0.1); /* 半透明背景 */
border-radius: 20px;
padding: 25px;
box-shadow: 0 10px 30px var(--shadow-color);
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around; /* 元素垂直居中分布 */
position: relative;
overflow: hidden; /* 确保内部元素不超出圆角 */
transition: transform var(--transition-speed) ease,
box-shadow var(--transition-speed) ease,
background var(--transition-speed) ease;
backdrop-filter: blur(var(--blur-amount)); /* 毛玻璃模糊效果 */
border: 1px solid rgba(255, 255, 255, 0.2);
cursor: pointer; /* 提示可交互 */
}
/* 卡片悬停效果 */
.weather-card:hover {
transform: translateY(-15px) scale(1.03);
box-shadow: 0 25px 45px rgba(0, 0, 0, 0.5);
}
/* 卡片内容 */
.weather-card .card-content {
position: relative;
z-index: 2; /* 确保内容在覆盖层之上 */
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.weather-card .icon-container {
font-size: 4rem; /* 图标大小 */
margin-bottom: 15px;
display: inline-block; /* 允许动画和阴影 */
position: relative;
text-shadow: 0 5px 15px rgba(0,0,0,0.3);
}
.weather-card .icon {
display: inline-block; /* 确保动画应用 */
}
.weather-card .city-name {
font-size: 1.8rem;
font-weight: 700;
margin-bottom: 5px;
text-shadow: 0 3px 10px rgba(0,0,0,0.2);
}
.weather-card .temperature {
font-size: 3.5rem;
font-weight: 300;
line-height: 1;
margin-bottom: 10px;
text-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
.weather-card .description {
font-size: 1.2rem;
font-weight: 400;
color: rgba(255, 255, 255, 0.9);
margin-bottom: 20px;
text-shadow: 0 2px 8px rgba(0,0,0,0.2);
}
.weather-card .details {
font-size: 0.9rem;
color: rgba(255, 255, 255, 0.8);
display: flex;
gap: 20px;
margin-top: auto; /* 挤到底部 */
width: 100%;
justify-content: center;
}
.weather-card .details span {
display: inline-block;
padding: 3px 8px;
background: rgba(255, 255, 255, 0.1);
border-radius: 5px;
}
/* 卡片覆盖层和发光效果 */
.card-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
opacity: 0; /* 默认隐藏 */
transition: opacity var(--transition-speed) ease;
}
.card-glow {
position: absolute;
top: 50%;
left: 50%;
width: 150px;
height: 150px;
background: rgba(255, 255, 255, 0.3);
border-radius: 50%;
filter: blur(30px);
z-index: 0;
opacity: 0;
transition: opacity var(--transition-speed) ease, transform var(--transition-speed) ease;
}
.weather-card:hover .card-overlay,
.weather-card:hover .card-glow {
opacity: 1;
}
.weather-card:hover .card-glow {
transform: translate(-50%, -50%) scale(1.5);
}
/* 不同天气类型的样式 */
/* 晴天 */
.weather-card.sunny { background: linear-gradient(135deg, var(--sunny-color-start) 0%, var(--sunny-color-end) 100%); }
.weather-card.sunny .icon::before { content: '☀️'; }
.weather-card.sunny .icon-container { color: #FFD700; }
.weather-card.sunny .card-glow { background: rgba(255, 215, 0, 0.3); }
.weather-card.sunny .description { color: rgba(255, 255, 255, 0.9); }
/* 大风 */
.weather-card.windy { background: linear-gradient(135deg, var(--windy-color-start) 0%, var(--windy-color-end) 100%); }
.weather-card.windy .icon::before { content: '💨'; }
.weather-card.windy .icon-container { color: #FFFFFF; }
.weather-card.windy .card-glow { background: rgba(255, 255, 255, 0.2); }
.weather-card.windy .icon { animation: windPulse 2s infinite ease-in-out; }
.weather-card.windy .details span:first-child { background: rgba(255, 255, 255, 0.2); }
/* 暴雨 */
.weather-card.rainy { background: linear-gradient(135deg, var(--rainy-color-start) 0%, var(--rainy-color-end) 100%); }
.weather-card.rainy .icon::before { content: '🌧️'; }
.weather-card.rainy .icon-container { color: #ADD8E6; }
.weather-card.rainy .card-glow { background: rgba(173, 216, 230, 0.2); }
.weather-card.rainy .icon { animation: rainDrop 1.5s infinite linear; }
.weather-card.rainy .card-content::after { /* 添加下雨的视觉效果 */
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: repeating-linear-gradient(
to bottom,
transparent,
transparent 4px,
rgba(173, 216, 230, 0.3) 5px,
transparent 6px
);
opacity: 0.6;
z-index: 1;
pointer-events: none; /* 不干扰交互 */
}
/* 暴雪 */
.weather-card.snowy { background: linear-gradient(135deg, var(--snowy-color-start) 0%, var(--snowy-color-end) 100%); }
.weather-card.snowy .icon::before { content: '❄️'; }
.weather-card.snowy .icon-container { color: #E0FFFF; }
.weather-card.snowy .card-glow { background: rgba(224, 240, 255, 0.2); }
.weather-card.snowy .icon { animation: snowFlake 2.5s infinite linear; }
.weather-card.snowy .description { font-weight: bold; }
/* 占位符卡片 */
.weather-card.placeholder {
justify-content: center;
background: rgba(255, 255, 255, 0.1);
border: 1px dashed rgba(255, 255, 255, 0.3);
box-shadow: none;
backdrop-filter: blur(5px);
}
.weather-card.placeholder p {
font-size: 1.5rem;
color: rgba(255, 255, 255, 0.7);
}
/* 页脚 */
footer {
margin-top: 40px;
font-size: 0.9rem;
color: rgba(255, 255, 255, 0.7);
text-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
/* 动画定义 */
@keyframes floatText {
0% { transform: translateY(0px) scale(1); opacity: 1; }
50% { transform: translateY(-10px) scale(1.02); opacity: 0.9; }
100% { transform: translateY(0px) scale(1); opacity: 1; }
}
@keyframes windPulse {
0% { transform: scale(1); opacity: 0.7; }
50% { transform: scale(1.1); opacity: 1; }
100% { transform: scale(1); opacity: 0.7; }
}
@keyframes rainDrop {
0% { transform: translateY(-50px); opacity: 0; }
20% { opacity: 1; }
80% { opacity: 0.8; }
100% { transform: translateY(350px); opacity: 0; } /* 确保落到卡片底部以下 */
}
@keyframes snowFlake {
0% { transform: translateY(-50px) rotate(0deg); opacity: 0; }
10% { opacity: 1; }
70% { opacity: 0.8; }
100% { transform: translateY(450px) rotate(180deg); opacity: 0; } /* 确保落到底部以下并有旋转 */
}
/* 响应式调整 (可选) */
@media (max-width: 768px) {
header h1 {
font-size: 2.2rem;
}
.weather-card {
flex: 0 0 250px; /* 小屏幕卡片宽度 */
height: 350px;
}
.weather-card .icon-container {
font-size: 3.5rem;
}
.weather-card .temperature {
font-size: 2.8rem;
}
.weather-cards-container {
gap: 15px;
padding-left: 15px; /* 避免左侧边缘贴合 */
padding-right: 15px;
justify-content: flex-start; /* 在小屏幕上靠左排列,便于滚动 */
}
.select-wrapper {
padding: 8px 15px;
}
}
@media (max-width: 480px) {
body {
align-items: flex-start; /* 移动端顶部对齐 */
padding-top: 50px;
}
header h1 {
font-size: 1.8rem;
margin-bottom: 20px;
}
.weather-cards-container {
gap: 10px;
padding-left: 10px;
padding-right: 10px;
height: 380px; /* 给单卡片留足够高度 */
align-items: center; /* 垂直居中单卡片 */
}
.weather-card {
flex: 0 0 85%; /* 移动端卡片占屏幕大部分宽度 */
max-width: 300px;
margin: 0 auto; /* 居中 */
height: 360px;
}
.select-wrapper {
padding: 6px 12px;
}
#city-select {
font-size: 1rem;
}
}
JavaScript (script.js):
document.addEventListener('DOMContentLoaded', () => {
const citySelect = document.getElementById('city-select');
const weatherCardMain = document.getElementById('weather-card-main');
const placeholderCard = document.getElementById('placeholder-card');
// 模拟的天气数据 API
const weatherData = {
beijing: { temp: 15, description: '晴朗', condition: 'sunny', wind: 5, humidity: 40 },
shanghai: { temp: 22, description: '小雨', condition: 'rainy', wind: 10, humidity: 85 },
guangzhou: { temp: 28, description: '多云', condition: 'sunny', wind: 8, humidity: 70 }, // 假设多云也用晴天卡外观
london: { temp: 10, description: '大风', condition: 'windy', wind: 25, humidity: 60 },
tokyo: { temp: 8, description: '雨夹雪', condition: 'snowy', wind: 15, humidity: 75 }, // 假设雨夹雪用暴雪卡外观
newyork: { temp: -2, description: '暴雪', condition: 'snowy', wind: 20, humidity: 90 }
};
citySelect.addEventListener('change', (event) => {
const selectedCity = event.target.value;
// 隐藏占位符,显示主卡片
placeholderCard.style.display = 'none';
weatherCardMain.style.display = 'block';
if (selectedCity && weatherData[selectedCity]) {
const data = weatherData[selectedCity];
// 更新卡片内容
const cardMainElement = weatherCardMain.querySelector('.card-content');
cardMainElement.querySelector('.city-name').textContent = selectedCity.toUpperCase();
cardMainElement.querySelector('.temperature').textContent = `${data.temp}°C`;
cardMainElement.querySelector('.description').textContent = data.description;
cardMainElement.querySelector('.wind-speed').textContent = `${data.wind} km/h`;
cardMainElement.querySelector('.humidity').textContent = `${data.humidity}%`;
// 更新卡片样式 (关键步骤)
// 先移除所有天气类,再添加对应类
weatherCardMain.className = 'weather-card'; // 重置基础类
// 使用 requestAnimationFrame 确保样式更新在类添加之前应用,避免过渡闪烁
requestAnimationFrame(() => {
weatherCardMain.classList.add(data.condition);
});
} else {
// 如果未选择或数据无效,则显示占位符
placeholderCard.style.display = 'block';
weatherCardMain.style.display = 'none';
}
});
// 可选:页面加载时自动选择第一个城市或显示占位符
// if (citySelect.value) {
// citySelect.dispatchEvent(new Event('change'));
// }
});
如何运行:
- 将以上 HTML 代码保存为
index.html。
- 将 CSS 代码保存为
style.css,并放在与 index.html 同一个文件夹下。
- 将 JavaScript 代码保存为
script.js,同样放在 index.html 同一个文件夹下。
- 用浏览器打开
index.html 文件。
效果说明:
- 整体布局: 页面采用居中布局,背景是优雅的渐变色。
- 城市选择: 一个下拉菜单用于选择城市。
- 天气卡片:
- 基础样式: 卡片有圆角、半透明背景和模糊(毛玻璃)效果,营造出悬浮感和高级感。
- 阴影与悬停: 使用
box-shadow 增加深度,并且在鼠标悬停 (hover) 时,卡片会轻微上移、放大,阴影加深,视觉冲击力更强。.card-glow 和 .card-overlay 在悬停时出现,增加了动态的光晕效果。
- 内容展示: 卡片清晰展示城市名称、温度、天气描述、风速和湿度。
- 动态元素 & 动画:
- 图标: 根据天气条件显示不同的 Emoji(☀️, 💨, 🌧️, ❄️),并通过 CSS
@keyframes 添加了微妙的动画效果(如晴天的光晕感,大风的脉动,雨滴的下落,雪花的旋转飘落)。
- 背景渐变: 每种天气类型(晴天、大风、暴雨、暴雪)都有独特的背景渐变色,视觉上区分明显。
- 特殊效果: 暴雨卡片添加了模拟的下雨条纹背景,增加了沉浸感。
- UX 优化:
- 默认显示占位符卡片,引导用户进行选择。
- 选择城市后,占位符消失,天气卡片出现并更新内容和样式。
- 使用了
requestAnimationFrame 来确保在添加天气类名时,CSS 过渡能正确应用,避免闪烁。
- 添加了响应式设计,确保在不同尺寸屏幕上都有较好的观看体验。
这个实现旨在提供一个视觉上吸引人且交互流畅的天气卡片体验,满足了您对华丽外观、优雅动画、动态元素和沉浸式体验的要求。您可以根据需要调整颜色、动画速度和细节。