-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
76 lines (60 loc) · 2.53 KB
/
weather.js
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
`use strict`
const container = document.querySelector('.container');
const search = document.querySelector('.search_box button');
const weatherBox = document.querySelector('.weather_box');
const weatherDetails = document.querySelector('.weather_details');
const error404 = document.querySelector('.not_found')
search.addEventListener('click',() => {
const APIkey = '8b50bb88133ad6fda215d17d779ea295'
const city = document.querySelector('.search_box input').value
if (city == '')
return;
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${APIkey}`).then(response => response.json()).then(json => {
if (json.cod == '404'){
container.style.height = '400px';
weatherBox.classList.remove('active');
weatherDetails.classList.remove('active');
error404.classList.add('active');
return;
}
container.style.height = '555px';
weatherBox.classList.add('active');
weatherDetails.classList.add('active');
error404.classList.remove('active' );
const image = document.querySelector('.weather_box img');
const temperature = document.querySelector('.temperature');
const description = document.querySelector('.weather_box .description');
const humidity = document.querySelector(' .humidity span');
const wind = document.querySelector('.wind span');
switch(json.weather[0].main){
case 'Rain':
image.src = 'rain.svg';
break;
case 'Cloud':
image.src = 'cloud.svg';
break;
case 'Mist':
image.src = 'mist.svg';
break;
case 'Snow':
image.src = 'snow.svg';
break;
case 'Night':
image.src = 'nighttime.png';
break;
case 'Haze':
image.src = 'haze.svg';
break;
case 'Clear':
image.src = 'clear.svg';
break;
case 'Storm':
image.src = 'storm.svg';
break;
}
temperature.innerHTML = `${parseInt(json.main.temp)}°C`;
description.innerHTML = `${json.weather[0].description}`;
humidity.innerHTML = `${json.main.humidity}%`;
wind.innerHTML = `${parseInt(json.wind.speed)}Km/h`
});
});