-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (94 loc) · 2.72 KB
/
index.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
const api = {
key: "2fa73590fd8b5a4c6e68098ad5625395",
base: "https://api.openweathermap.org/data/2.5/",
};
getResults("Karachi");
const searchbox = document.querySelector(".search-box");
searchbox.addEventListener("keypress", setQuery);
function setQuery(evt) {
if (evt.keyCode == 13) {
getResults(searchbox.value);
}
}
function getResults(query) {
fetch(`${api.base}weather?q=${query}&units=metric&APPID=${api.key}`)
.then((weather) => {
return weather.json();
})
.then(displayResults);
}
function displayResults(weather) {
//console.log(weather);
let city = document.querySelector(".location .city");
city.innerText = `${weather.name}, ${weather.sys.country}`;
let now = new Date();
let date = document.querySelector(".date");
date.innerText = dateBuilder(now);
let temp = document.querySelector(".temp");
temp.innerHTML = `${Math.round(weather.main.temp)}<span>℃</span>`;
let Weather = document.querySelector(".weather");
Weather.innerText = weather.weather[0].main;
bgSetter(weather);
let hilow = document.querySelector(".hi-low");
hilow.innerText = `${Math.round(weather.main.temp_min)} ℃ / ${Math.round(
weather.main.temp_max
)} ℃`;
}
function bgSetter(weather) {
const current = weather.weather[0].main;
console.log(current);
let body = document.getElementById("body");
if (current == "Clear") {
body.style.background = `url("Clear.jpg")`;
} else if (current == "Snow") {
body.style.background = `url("Snow.jpg")`;
} else if (current == "Clouds") {
body.style.background = `url("Cloudy.jpg")`;
} else if (current == "Fog") {
body.style.background = `url("Fog.jpg")`;
} else if (current == "Haze") {
body.style.background = `url("Haze.jpg")`;
} else if (current == "Rain") {
body.style.background = `url("Rain.jpg")`;
} else if (current == "Smoke") {
body.style.background = `url("Smoke.jpg")`;
} else if (current == "Sunny") {
body.style.background = `url("Sunny.jpg")`;
} else if ("Thunder" in current) {
body.style.background = `url("Clear.jpg")`;
} else if ("wind" in current) {
body.style.background = `url("Windy.jpg")`;
}
body.style.backgroundSize = `cover`;
body.style.backgroundPosition = `top center`;
}
function dateBuilder(d) {
let months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
let days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
let day = days[d.getDay()];
let month = months[d.getMonth()];
let date = d.getDate();
let year = d.getFullYear();
return `${day}, ${date} ${month} ${year}`;
}