-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (92 loc) · 3.74 KB
/
app.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
window.addEventListener('load', ()=> {
let lat;
let long;
let tempDesc = document.querySelector(".temp-desc");
let timeZone = document.querySelector(".tz");
let temperature = document.querySelector(".temp-degree");
let tempDegree = document.querySelector(".degree");
let tempUnit = document.querySelector(".unit");
let tempMemo = document.querySelector(".memo");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
lat = position.coords.latitude;
long = position.coords.longitude;
// const api = `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${long}¤t_weather=true&hourly=weathercode`;
const key = '13584083f1aa84005931e77de9107749';
const api = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${key}`;
fetch(api)
.then(response => {
return response.json();
})
.then(data => {
// console.log(data);
const kelvin = data.main.temp;
const celsius = kelvin - 273.15;
const icon = data.weather[0].description;
tempDegree.textContent = Math.floor(celsius) + '°';
tempDesc.textContent = icon;
timeZone.textContent = data.name;
setIcons(icon, document.querySelector('.icon1'));
temperature.addEventListener('click', function() {
if(tempUnit.textContent === "C") {
tempUnit.textContent = "F";
const fah = (celsius * 9/5) + 32;
tempDegree.textContent = Math.floor(fah) + '°';
tempMemo.textContent = "Click to celsius";
} else {
tempUnit.textContent = "C";
tempDegree.textContent = Math.floor(celsius) + '°';
tempMemo.textContent = "Click to fahrenheit";
}
});
});
});
} else {
alert('cannot find your coordiantes');
};
function setIcons(icon, iconID) {
const skycons = new Skycons({color: "white"});
switch (icon) {
case "clear sky":
icon = "clear_day";
break;
case "few clouds":
icon = "partly_cloudy_day";
break;
case "scattered clouds":
case "broken clouds":
icon = "cloudy";
break;
case "shower rain":
icon = "rain";
break;
case "rain":
case "thunderstorm":
icon = "sleet";
break;
case "mist":
case "haze":
icon = "fog";
break;
}
setColor(icon);
const currentIcon = icon.toUpperCase();
skycons.play();
return skycons.set(iconID, Skycons[currentIcon]);
}
function setColor(mood) {
if (mood === "clear_day") {
document.body.classList.add("clear-day");
} else if (mood === "partly_cloudy_day") {
document.body.classList.add("partly-cloudy");
} else if (mood === "cloudy") {
document.body.classList.add('cloudy');
} else if (mood === "rain") {
document.body.classList.add('rain');
} else if (mood === "sleet") {
document.body.classList.add('sleet');
} else if (mood === "snow") {
document.body.classList.add('snow');
}
}
});