-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
56 lines (45 loc) · 1.45 KB
/
script.js
File metadata and controls
56 lines (45 loc) · 1.45 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
const input = document.getElementById('input');
const grid = document.getElementsByClassName('grid')[0];
window.addEventListener('load', dayNightMode)
input.addEventListener('keydown', function(event){
if(event.key == 'Enter')
loadImg();
})
function loadImg(){
removeImages()
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)
return response.json();
else
alert(response.status)
})
.then(data => {
const imageNodes = [];
for(let i = 0; i < data.results.length;i++){
imageNodes[i] = document.createElement('div');
imageNodes[i].className = 'img';
imageNodes[i].style.backgroundImage = "url("+data.results[i].urls.raw+')';
imageNodes[i].addEventListener("dblclick", function(){
window.open(data.results[i].links.download, '_blank');
})
grid.appendChild(imageNodes[i]);
}
})
}
function removeImages(){
grid.innerHTML = '';
}
function dayNightMode(){
let date = new Date();
let hour = date.getHours();
if(hour >= 7 && hour <= 19){
document.body.style.backgroundColor = 'whitesmoke';
document.body.style.color = 'black';
}
else{
document.body.style.backgroundColor = 'black';
document.body.style.color = 'white';
}
}