-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetching.js
72 lines (58 loc) · 1.94 KB
/
fetching.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
//if you have promblems with the fetching images - its probably becouse of the "Rate Limit Exceeded",
//please copy and paste another client id and try again
// first - 896d4f52c589547b2134bd75ed48742db637fa51810b49b607e37e46ab2c0043
// second - s2iDMNtNY-yGTP-Q8T1X3dNDY8Dw3vzuBE6T1ia07hg
const fetchImagesContainer = document.getElementById("fetchImages");
const popup = document.getElementById("popup");
const popupImage = document.getElementById("popupImage");
const closePopup = document.getElementById("closePopup");
let currentCount = 10;
async function fetchImages() {
const response = await fetch(
`https://api.unsplash.com/photos/random?client_id=s2iDMNtNY-yGTP-Q8T1X3dNDY8Dw3vzuBE6T1ia07hg&count=${currentCount}`
);
const data = await response.json();
return data;
}
function createPhotoElement(photo) {
const img = document.createElement("img");
img.src = photo.urls.small;
img.alt = photo.alt_description;
img.classList.add("photo");
img.addEventListener("load", () => {
new Masonry(fetchImagesContainer, {
itemSelector: ".photo",
columnWidth: ".photo",
gutter: 0,
});
});
img.addEventListener("click", () => {
openPopup(photo.urls.full);
});
return img;
}
function openPopup(imageUrl) {
popupImage.src = imageUrl;
popup.style.display = "flex";
}
closePopup.addEventListener("click", () => {
popup.style.display = "none";
});
async function displayImages() {
try {
const images = await fetchImages();
images.forEach((photo) => {
const photoElement = createPhotoElement(photo);
fetchImagesContainer.appendChild(photoElement);
});
} catch (error) {
console.error("An error occurred:", error);
}
}
//loadmore--------------------------------------------------------------------
const loadMoreButton = document.getElementById("loadMore");
loadMoreButton.addEventListener("click", () => {
currentCount = currentCount + 3;
displayImages();
});
displayImages();