-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
96 lines (85 loc) · 2.63 KB
/
Copy pathapp.js
File metadata and controls
96 lines (85 loc) · 2.63 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
async function weather() {
const success = (position) => {
const long = position.coords.longitude;
const lat = position.coords.latitude;
const key = "API_KEY";
fetch(
`https://api.weatherbit.io/v2.0/current?lat=${lat}&lon=${long}&key=${key}&include=minutely`
)
.then((response) => response.json())
.then((data) => {
console.log(data);
// set location
document.querySelector(".location").innerText = data.data[0].city_name;
document.querySelector(
".location"
).href = `https://www.google.com/maps/place/${data.data[0].city_name}`;
// set date
const today = new Date();
const monthNames = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
const date = today.getDate() + "/" + monthNames[today.getMonth()];
document.querySelector(".date").innerText =
date +
" - " +
(today.getHours() % 12) +
":" +
today.getMinutes() +
" " +
getZone();
function getZone() {
if (today.getHours <= 12) return "am";
else return "pm";
}
// set temp
document.querySelector(".temp").innerText =
parseInt(data.data[0].temp) + "॰";
// set desc
document.querySelector(".sky").innerText =
data.data[0].weather.description;
// set wind speed
document.querySelector(".wind").innerText =
Math.round(data.data[0].wind_spd * 100) / 100 + " m/s";
// set humid
document.querySelector(".humid").innerText =
Math.round(data.data[0].rh * 100) / 100 + " %";
// set logo
document.querySelector(
".logo img"
).src = `https://www.weatherbit.io/static/img/icons/${data.data[0].weather.icon}.png`;
// setting bg
document.documentElement.style.setProperty("--bg", getBg());
// getBg
function getBg() {
if (today.getHours() < 14 && today.getHours() > 5) {
return "var(--morning)";
} else if (today.getHours() < 18 && today.getHours() >= 14) {
return "var(--evening)";
} else {
return "var(--night)";
}
}
})
.then(() => {
//appear everything
document.querySelector(".bg-img").style.display = "none";
});
};
const error = (error) => {
console.log(error);
};
navigator.geolocation.getCurrentPosition(success, error);
}
weather();