-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
86 lines (71 loc) · 2.79 KB
/
scripts.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
const IMAGE_COUNT = 50;
const IMAGES_DIRECTORY = "images/";
const IMAGES_EXTENSION = ".jpg";
const bodyElement = document.querySelector("body");
const gallery = document.querySelector(".gallery");
const overlay = document.querySelector(".overlay");
const viewer = document.querySelector(".viewer");
const viewerImage = document.querySelector(".viewerImage");
const navElement = document.querySelector(".nav");
function showViewer() {
overlay.style.display = "block";
bodyElement.style.overflow = "hidden";
viewerImage.src = this.src;
this.classList.add("viewing");
}
function hideViewer() {
overlay.style.display = "none";
bodyElement.style.overflow = "visible";
const lastImage = document.querySelector(".viewing");
lastImage.classList.remove("viewing");
}
function addImageToGallery(imageLink) {
const image = document.createElement("img");
image.className = "galleryImage";
image.setAttribute("loading", "lazy");
image.src = imageLink;
image.addEventListener("click", showViewer);
gallery.append(image);
}
// adding sample images to gallery
for (let index = 1; index <= IMAGE_COUNT; index++) {
const sampleImageLink = `${IMAGES_DIRECTORY}${String(
index
)}${IMAGES_EXTENSION}`;
addImageToGallery(sampleImageLink);
}
// viewer nav
const navBackward = document.querySelector(".nav-backward");
const navSpacer = document.querySelector(".nav-spacer");
const navForward = document.querySelector(".nav-forward");
function changeViewerImage(delta) {
const currentImage = document.querySelector(".viewing");
if (delta === -1 || delta === 1) {
let nextImage;
if (delta === -1) {
nextImage = currentImage.previousElementSibling;
if (nextImage === null || nextImage === undefined)
nextImage = currentImage.parentNode.lastChild;
viewerImage.src = nextImage.src;
} else if (delta === 1) {
nextImage = currentImage.nextElementSibling;
if (nextImage === null || nextImage === undefined)
nextImage = currentImage.parentNode.firstChild;
}
viewerImage.src = nextImage.src;
currentImage.classList.remove("viewing");
nextImage.classList.add("viewing");
}
}
document.addEventListener("keydown", (e) => {
const key = e.key.toLowerCase();
if (key.includes("left")) changeViewerImage(-1);
else if (key.includes("right")) changeViewerImage(1);
if (key === "escape") hideViewer();
});
navBackward.addEventListener("click", () => changeViewerImage(-1));
navForward.addEventListener("click", () => changeViewerImage(1));
overlay.addEventListener("click", hideViewer);
// masks the above listener just in case the user
// clicks on the viewer image
navElement.addEventListener("click", (e) => e.stopPropagation());