forked from livebloggerofficial/Image-Gallery-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
49 lines (42 loc) · 1.4 KB
/
main.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
const lightbox = document.querySelector(".image-gallery-container .lightbox");
const lightboxImage = document.querySelector(
".image-gallery-container .lightbox img"
);
const lightboxTitle = document.querySelector(
".image-gallery-container .lightbox .title"
);
const downloadBtn = document.querySelector(
".image-gallery-container .download-btn"
);
const nextBtn = document.querySelector(".image-gallery-container .next-btn");
const previousBtn = document.querySelector(
".image-gallery-container .previous-btn"
);
const closeBtn = document.querySelector(".image-gallery-container .close-btn");
let currentImage = "";
const showImage = (data) => {
currentImage = data;
lightbox.classList.add("active");
let image = data.querySelector("img");
let title = data.querySelector(".title").innerText;
lightboxImage.src = image.src;
downloadBtn.href = image.src;
if (title) {
lightboxTitle.innerText = title;
}
};
closeBtn.addEventListener("click", () => {
lightbox.classList.remove("active");
});
nextBtn.addEventListener("click", () => {
if (currentImage.nextElementSibling) {
currentImage = currentImage.nextElementSibling;
showImage(currentImage);
}
});
previousBtn.addEventListener("click", () => {
if (currentImage.previousElementSibling) {
currentImage = currentImage.previousElementSibling;
showImage(currentImage);
}
});