-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
148 lines (107 loc) · 5.07 KB
/
script.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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');
const cityHide = document.querySelector('.city-hide');
search.addEventListener('click' , ()=>{
const APIKey = '199e2d8725e579bfbdd6fb50a237ca07';
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'){
cityHide.textContent = city;
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('.weather-box .temperature');
const description = document.querySelector('.weather-box .description');
const humidity = document.querySelector('.weather-details .humidity span');
const wind = document.querySelector('.weather-details .wind span');
if(cityHide.textContent==city){
return;
}
else{
cityHide.textContent=city;
container.style.height = '555px';
container.classList.add('active');
weatherBox.classList.add('active');
weatherDetails.classList.add('active');
error404.classList.remove('active');
setTimeout(()=>{
container.classList.remove('active');
},2500);
switch(json.weather[0].main){
case 'Clear':
image.scr='Images/clear.png';
break;
case 'Rain':
image.src='Images/rain.png';
break;
case 'Snow':
image.src='Images/snow.png';
break;
case 'Clouds':
image.src='Images/cloud.png';
break;
case 'Mist':
image.src='Images/mist.png';
break;
case 'Haze':
image.src='Images/mist.png';
break;
default:
image.src='Images/cloud.png';
}
temperature.innerHTML=`${parseInt(json.main.temp)}<span>°C</span>`;
description.innerHTML=`${json.weather[0].description}`;
humidity.innerHTML=`${json.main.humidity}%`;
wind.innerHTML=`${parseInt(json.wind.speed)}km/h`;
const infoWeather = document.querySelector('.info-weather');
const infoHumidity = document.querySelector('.info-humidity');
const infoWind = document.querySelector('.info-wind');
const elCloneInfoWeather = infoWeather.cloneNode(true);
const elCloneInfoHumidity = infoHumidity.cloneNode(true);
const elCloneInfoWind = infoWind.cloneNode(true);
elCloneInfoWeather.id = 'clone-info-weather';
elCloneInfoWeather.classList.add('active-clone');
elCloneInfoHumidity.id = 'clone-info-humidity';
elCloneInfoHumidity.classList.add('active-clone');
elCloneInfoWind.id = 'clone-info-wind';
elCloneInfoWind.classList.add('active-clone');
setTimeout(()=>{
infoWeather.insertAdjacentElement("afterend" , elCloneInfoWeather);
infoHumidity.insertAdjacentElement("afterend" , elCloneInfoHumidity);
infoWind.insertAdjacentElement("afterend" , elCloneInfoWind);
},2200);
const cloneInfoWeather = document.querySelectorAll('.info-weather.active-clone');
const totalCloneInfoWeather = cloneInfoWeather.length;
const cloneInfoWeatherFirst = cloneInfoWeather[0];
const cloneInfoHumidity = document.querySelectorAll('.info-humidity.active-clone');
const totalCloneInfoHumidity = cloneInfoHumidity.length;
const cloneInfoHumidityFirst = cloneInfoHumidity[0];
const cloneInfoWind = document.querySelectorAll('.info-wind.active-clone');
const totalCloneInfoWind = cloneInfoWind.length;
const cloneInfoWindFirst = cloneInfoWind[0];
if(totalCloneInfoWeather>0){
cloneInfoWeatherFirst.remove('active-clone');
cloneInfoHumidityFirst.remove('active-clone');
cloneInfoWindFirst.remove('active-clone');
setTimeout(()=>{
cloneInfoWeatherFirst.remove();
cloneInfoHumidityFirst.remove();
cloneInfoWindFirst.remove();
},2200);
}
}
});
});