-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (82 loc) · 2.64 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
const loader = document.querySelector(".loader-container");
const container = document.querySelector(".container");
const svgIcon = `<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="download"
>
<line x1="12" y1="5" x2="12" y2="19"></line>
<polyline points="19 12 12 19 5 12"></polyline>
</svg>`;
const accessKey = "bOmWUNA52SyWN5ZXLLOQfJyWZRFj7VVn3iqyU9tZbhE";
const secretKey = "r6Ij4q4LNUeA1AeMcUklYOJVP7PERs7kxkWhqtq9keA";
const count = 30;
let photosArray = [];
let ready = false;
let imagesLoads = 0;
let totalImages = 0;
const apiUrl = `https://api.unsplash.com/photos/random/?client_id=${accessKey}&count=${count}`;
function imageLoaded() {
loader.hidden = true;
imagesLoads++;
console.log(imagesLoads);
if ((imagesLoads = totalImages)) {
ready = true;
console.log("ready", ready);
}
}
function displayPhotos() {
imagesLoads = 0;
totalImages = photosArray.length;
photosArray.forEach((photo) => {
const item = document.createElement("div");
item.setAttribute("class", "image-box");
const image = document.createElement("img");
image.setAttribute("src", photo.urls.regular);
const overlay = document.createElement("div");
overlay.setAttribute("class", "overlay");
const altInfo = document.createElement("span");
altInfo.setAttribute("class", "alt-info");
altInfo.textContent = photo.user.name;
const downloadLink = document.createElement("a");
downloadLink.setAttribute("href", photo.links.download);
downloadLink.setAttribute("class", "download-link");
downloadLink.setAttribute("target", "_blank");
item.appendChild(image);
item.appendChild(overlay);
overlay.appendChild(altInfo);
downloadLink.insertAdjacentHTML("afterbegin", svgIcon);
overlay.appendChild(downloadLink);
container.appendChild(item);
image.addEventListener("load", imageLoaded);
});
}
async function getPhotos() {
try {
const res = await fetch(apiUrl);
photosArray = await res.json();
// console.log(photosArray);
displayPhotos();
} catch (error) {
console.log(error);
}
}
// check if scrollbar at bottom then get photos again
document.addEventListener("scroll", (scroll) => {
if (
window.innerHeight + window.scrollY >= document.body.offsetHeight - 1000 &&
ready
) {
ready = false;
getPhotos();
// console.log("getting photos again");
}
});
getPhotos();