-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
187 lines (128 loc) · 5.99 KB
/
script.js
File metadata and controls
187 lines (128 loc) · 5.99 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// document.addEventListener('DOMContentLoaded', function() {
// // Create the map
// var map = L.map('map').setView([51.505, -0.09], 13);
// // Add the tile layer
// L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
// attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
// }).addTo(map);
// // Add a marker
// L.marker([51.5, -0.09]).addTo(map)
// .bindPopup('A pretty CSS3 popup.<br> Easily customizable.')
// .openPopup();
// });
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');
const closeButton = document.querySelector('.icon-close');
const weatherButton = document.querySelector('.btnWeather-button');
weatherButton.addEventListener('click', () => {
container.style.transform = 'scale(1)';
});
closeButton.addEventListener('click', () => {
container.style.transform = 'scale(0)';
});
search.addEventListener('click', () => {
const APIKey = '12a52ad57b89976739f447014199ddd6';
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;
}
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.src = 'images/clear.png';
break;
case 'Rain':
image.src = 'images/rain.png';
break;
case 'Snow':
image.src = 'images/snow.png';
break;
case 'Clouds':
mage.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>℃</span>';
description.innerHTML = '${json.weather[0].description}';
humidity.innerHTML = '${json.main.humidity}%';
wind.innerHTML = '${pareseInt(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 cloneInfoHumidityFirst = cloneInfoHumidity[0];
const cloneInfoWind = document.querySelectorAll('.info-wind.active-clone');
const cloneInfoWindFirst = cloneInfoWind[0];
if (totalCloneInfoWeather > 0) {
cloneInfoWeatherFirst.classList.remove('active-clone');
cloneInfoHumidityFirst.classList.remove('active-clone');
cloneInfoWindFirst.classList.remove('active-clone');
setTimeout(() => {
cloneInfoWeatherFirst.remove();
cloneInfoHumidityFirst.remove();
cloneInfoWindFirst.remove();
}, 2200);
}
}
});
// // Get the map container element
// const mapContainer = document.getElementById('map');
// // Create the map and set its dimensions to cover the entire page
// const map = L.map('map').setView([0, 0], 2); // Center the map at [0, 0] with a zoom level of 2 (world view)
// // Add the base map layer (e.g., OpenStreetMap)
// L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
// attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
// }).addTo(map);