-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (44 loc) · 1.58 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
dayNightTheme = () => {
let date = new Date();
let hour = date.getHours();
if (hour >= 7 && hour < 19) {
document.body.style.backgroundColor = 'white';
document.body.style.color = 'black';
}
else {
document.body.style.backgroundColor = 'black';
document.body.style.color = 'white';
}
}
window.addEventListener('load', dayNightTheme);
document.querySelector("#input").addEventListener("keydown", (event) => {
if (event.key == "Enter")
apiRequest();
});
document.querySelector("#search").addEventListener("click", () => {
apiRequest();
});
apiRequest = () => {
document.querySelector("#grid").textContent = "";
const url = 'https://api.unsplash.com/search/photos?query=' + input.value + '&per_page=30&client_id=SouHY7Uul-OxoMl3LL3c0NkxUtjIrKwf3tsGk1JaiVo';
fetch(url)
.then(response => {
if (!response.ok) throw Error(response.statusText);
return response.json();
})
.then(data => {
loadImages(data);
})
.catch(error => console.log(error));
}
loadImages = (data) => {
for (let i = 0; i < data.results.length; i++) {
let image = document.createElement("div");
image.className = "img";
image.style.backgroundImage = "url(" + data.results[i].urls.raw + "&w=1366&h=768" + ")";
image.addEventListener("dblclick", function() {
window.open(data.results[i].links.download, '_blank');
})
document.querySelector("#grid").appendChild(image);
}
}