-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
109 lines (95 loc) · 4.36 KB
/
script.js
File metadata and controls
109 lines (95 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const API_KEY = 'd8539e0bf712c9b117246a8f4a4ad85a';
function setDynamicBackground(condition) {
const body = document.body;
let gradient;
condition = condition.toLowerCase();
if (condition.includes('rain')) gradient = 'linear-gradient(135deg, #3a7bd5, #3a6073)';
else if (condition.includes('cloud')) gradient = 'linear-gradient(135deg, #757f9a, #d7dde8)';
else if (condition.includes('clear')) gradient = 'linear-gradient(135deg, #56ccf2, #2f80ed)';
else if (condition.includes('storm')) gradient = 'linear-gradient(135deg, #141E30, #243B55)';
else gradient = 'linear-gradient(135deg, #667eea, #764ba2)';
body.style.background = gradient;
}
function formatTime(timestamp) {
const date = new Date(timestamp * 1000);
return date.getHours() + ':00';
}
function formatDay(timestamp) {
const date = new Date(timestamp * 1000);
return date.toLocaleDateString('en-US', { weekday: 'short' });
}
function showWeather(data) {
document.getElementById('loader').style.display = 'none';
const weatherDiv = document.getElementById('weather');
weatherDiv.style.display = 'block';
const { name } = data.city;
document.getElementById('location').innerText = name;
const current = data.list[0];
const condition = current.weather[0].description;
document.getElementById('temp').innerText = Math.round(current.main.temp) + '°C';
document.getElementById('desc').innerText = condition;
document.getElementById('humidity').innerText = current.main.humidity + '%';
document.getElementById('wind').innerText = current.wind.speed + ' km/h';
document.getElementById('feels').innerText = Math.round(current.main.feels_like) + '°C';
document.getElementById('icon').src = `https://openweathermap.org/img/wn/${current.weather[0].icon}@2x.png`;
setDynamicBackground(condition);
const hourlyDiv = document.getElementById('hourly');
hourlyDiv.innerHTML = '';
for (let i = 0; i < 8; i++) {
const hour = data.list[i];
const card = document.createElement('div');
card.classList.add('card');
card.innerHTML = `
<h4>${formatTime(hour.dt)}</h4>
<img src="https://openweathermap.org/img/wn/${hour.weather[0].icon}.png" />
<p>${Math.round(hour.main.temp)}°C</p>
`;
hourlyDiv.appendChild(card);
}
const dailyDiv = document.getElementById('daily');
dailyDiv.innerHTML = '';
const days = {};
data.list.forEach(forecast => {
const date = new Date(forecast.dt_txt).toDateString();
if (!days[date]) days[date] = [];
days[date].push(forecast.main.temp);
});
Object.keys(days).slice(1, 4).forEach(day => {
const temps = days[day];
const avg = (temps.reduce((a, b) => a + b) / temps.length).toFixed(1);
const card = document.createElement('div');
card.classList.add('card');
card.innerHTML = `
<h4>${formatDay(new Date(day).getTime() / 1000)}</h4>
<p>${avg}°C</p>
`;
dailyDiv.appendChild(card);
});
}
function fetchWeatherByCity(city) {
if (!city) return alert('Please enter a city name!');
document.getElementById('loader').style.display = 'block';
document.getElementById('weather').style.display = 'none';
const url = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&units=metric&appid=${API_KEY}`;
fetch(url)
.then(res => res.json())
.then(data => {
if (data.cod !== '200') {
alert('City not found!');
document.getElementById('loader').style.display = 'none';
return;
}
showWeather(data);
})
.catch(err => {
alert('Failed to fetch weather data.');
console.error(err);
document.getElementById('loader').style.display = 'none';
});
}
const searchBtn = document.getElementById('searchBtn');
const cityInput = document.getElementById('cityInput');
searchBtn.addEventListener('click', () => fetchWeatherByCity(cityInput.value));
cityInput.addEventListener('keypress', e => {
if (e.key === 'Enter') fetchWeatherByCity(cityInput.value);
});