<!-- HTML大哥搭建的結構 -->
<div class="weather-app">
<header class="weather-header">
<h1>🌦️ 智慧天氣</h1>
<input type="text" id="city-input" placeholder="輸入城市名稱...">
<button id="search-btn">搜尋</button>
</header>
<main class="weather-main">
<div class="weather-display">
<div class="weather-icon" id="weather-icon">☀️</div>
<div class="temperature" id="temperature">25°C</div>
<div class="weather-desc" id="weather-desc">晴天</div>
</div>
<div class="weather-details">
<div class="weather-item">
<span>濕度</span>
<span id="humidity">60%</span>
</div>
<div class="weather-item">
<span>風速</span>
<span id="wind-speed">5 km/h</span>
</div>
<div class="weather-item">
<span>氣壓</span>
<span id="pressure">1013 hPa</span>
</div>
</div>
</main>
</div>
/* CSS二哥設計的美麗樣式 */
.weather-app {
background: linear-gradient(135deg, #74b9ff, #0984e3);
color: white;
border-radius: 20px;
padding: 2rem;
text-align: center;
box-shadow: 0 15px 35px rgba(116, 185, 255, 0.3);
backdrop-filter: blur(10px);
}
.weather-header {
margin-bottom: 2rem;
}
.weather-header h1 {
font-size: 2.5rem;
margin-bottom: 1rem;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
#city-input {
padding: 1rem;
border: none;
border-radius: 25px;
width: 200px;
margin-right: 1rem;
outline: none;
font-size: 1rem;
}
#search-btn {
background: rgba(255,255,255,0.2);
color: white;
border: 2px solid rgba(255,255,255,0.3);
padding: 1rem 2rem;
border-radius: 25px;
cursor: pointer;
transition: all 0.3s ease;
}
#search-btn:hover {
background: rgba(255,255,255,0.3);
transform: translateY(-2px);
}
.weather-icon {
font-size: 5rem;
margin: 1rem 0;
animation: float 3s ease-in-out infinite;
}
@keyframes float {
0%, 100% { transform: translateY(0px); }
50% { transform: translateY(-10px); }
}
.temperature {
font-size: 4rem;
font-weight: bold;
margin: 1rem 0;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.weather-details {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
margin-top: 2rem;
}
.weather-item {
background: rgba(255,255,255,0.2);
padding: 1.5rem;
border-radius: 15px;
backdrop-filter: blur(5px);
border: 1px solid rgba(255,255,255,0.3);
transition: transform 0.3s ease;
}
.weather-item:hover {
transform: scale(1.05);
}
// JavaScript小弟實現的互動功能
class WeatherApp {
constructor() {
this.initializeApp();
}
initializeApp() {
// 綁定事件監聽器
document.getElementById('search-btn').addEventListener('click', () => {
this.searchWeather();
});
document.getElementById('city-input').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
this.searchWeather();
}
});
// 載入預設城市天氣
this.loadDefaultWeather();
}
async searchWeather() {
const city = document.getElementById('city-input').value.trim();
if (!city) {
alert('請輸入城市名稱!');
return;
}
try {
// 模擬API請求
const weatherData = await this.fetchWeatherData(city);
this.updateWeatherDisplay(weatherData);
} catch (error) {
console.error('天氣資料獲取失敗:', error);
alert('無法獲取天氣資料,請檢查城市名稱');
}
}
async fetchWeatherData(city) {
// 模擬API回應資料
return new Promise((resolve) => {
setTimeout(() => {
const mockData = {
city: city,
temperature: Math.floor(Math.random() * 30) + 10,
description: ['晴天', '多雲', '小雨', '陰天'][Math.floor(Math.random() * 4)],
humidity: Math.floor(Math.random() * 40) + 40,
windSpeed: Math.floor(Math.random() * 20) + 5,
pressure: Math.floor(Math.random() * 50) + 1000,
icon: ['☀️', '⛅', '🌧️', '☁️'][Math.floor(Math.random() * 4)]
};
resolve(mockData);
}, 1000);
});
}
updateWeatherDisplay(data) {
// 更新DOM元素
document.getElementById('weather-icon').textContent = data.icon;
document.getElementById('temperature').textContent = `${data.temperature}°C`;
document.getElementById('weather-desc').textContent = data.description;
document.getElementById('humidity').textContent = `${data.humidity}%`;
document.getElementById('wind-speed').textContent = `${data.windSpeed} km/h`;
document.getElementById('pressure').textContent = `${data.pressure} hPa`;
// 添加更新動畫
this.animateUpdate();
}
animateUpdate() {
const elements = document.querySelectorAll('.weather-display > *, .weather-item');
elements.forEach((el, index) => {
el.style.animation = 'none';
setTimeout(() => {
el.style.animation = `fadeIn 0.5s ease ${index * 0.1}s both`;
}, 10);
});
}
loadDefaultWeather() {
// 載入預設城市(台北)的天氣
this.updateWeatherDisplay({
city: '台北',
temperature: 25,
description: '晴天',
humidity: 60,
windSpeed: 8,
pressure: 1013,
icon: '☀️'
});
}
}
// 初始化應用程式
document.addEventListener('DOMContentLoaded', () => {
new WeatherApp();
});