-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (63 loc) · 2.43 KB
/
index.js
File metadata and controls
77 lines (63 loc) · 2.43 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
const API_KEY = "2b64467a9bd78f29e39ae1cc6bfaf51c";
// Elementos do DOM
const elIconWeather = document.getElementById("icon-weather");
const elTemperature = document.getElementById("temperature");
const elLocal = document.getElementById("local");
const elHumidity = document.getElementById("humidity");
const elSpeedWind = document.getElementById("speed-wind");
const elCard = document.querySelector(".card");
// Função para buscar dados da API
function getData(local) {
const route = `https://api.openweathermap.org/data/2.5/weather?q=${local}&lang=pt_br&units=metric&appid=${API_KEY}`;
return fetch(route).then((response) => response.json());
}
// Função para carregar informações
function loadInformation() {
const value = document.querySelector('[name="local"]').value;
getData(value)
.then((data) => {
if (data.cod === "404") {
elCard.classList.remove("active");
return;
}
// Log para visualizar todos os dados retornados pela API
elCard.classList.add("active");
elTemperature.innerHTML = Math.floor(data.main.temp) + "°C";
elLocal.innerHTML = data.name;
elHumidity.innerHTML = data.main.humidity + "%";
elSpeedWind.innerHTML = data.wind.speed + " km/h";
const icon = data.weather[0].main.toLowerCase();
const src = `assets/icons/${icon}.png`;
elIconWeather.setAttribute("src", src);
fadeIn();
})
.catch((error) => {
console.error("Erro ao buscar dados:", error);
});
}
// Função para lidar com o envio do formulário
function handleSubmit(event) {
event.preventDefault();
fadeOut();
}
// Animação de entrada
function fadeIn() {
const timeline = gsap.timeline();
const configFrom = { y: -50 };
const configTo = { y: 0, duration: 0.4, opacity: 1, ease: "back" };
timeline.fromTo("#icon-weather", configFrom, configTo);
timeline.fromTo("#temperature", configFrom, configTo, 0.1);
timeline.fromTo("#local", configFrom, configTo, 0.2);
timeline.fromTo("footer", configFrom, configTo, 0.3);
}
// Animação de saída
function fadeOut() {
const timeline = gsap.timeline({ onComplete: loadInformation });
const config = { y: 50, duration: 0.4, opacity: 0, ease: "slow" };
timeline.to("footer", config);
timeline.to("#local", config, 0.1);
timeline.to("#temperature", config, 0.2);
timeline.to("#icon-weather", config, 0.3);
}
// Event Listener
document.querySelector("form").addEventListener("submit", handleSubmit);