-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
149 lines (125 loc) · 5.93 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
149
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 inputField = document.querySelector('.search-box input');
// Function to handle search
const handleSearch = () => {
const APIKey = "efec1c0be75efc1c254086ba9c476d7f";
const city = inputField.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 = "assets/clear.png";
break;
case 'Rain':
image.src = "assets/rain.png";
break;
case 'Snow':
image.src = "assets/snow.png";
break;
case 'Clouds':
image.src = "assets/cloud.png";
break;
case 'Mist':
image.src = "assets/mist.png";
break;
case 'Haze':
image.src = "assets/mist.png";
break;
default:
image.src = "assets/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 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);
}
}
})
.catch(error => {
console.error('Error fetching weather data:', error);
});
};
search.addEventListener('click', handleSearch);
inputField.addEventListener('keypress', (e) => {
if (e.keyCode === 13) {
// If Enter key is pressed
handleSearch();
}
});
// Event listener for the '/' key
document.addEventListener('keydown', (e) => {
if (e.key === '/') {
e.preventDefault(); // Preventing the default behavior of '/'
inputField.focus(); // Focus the search input field
}
});