-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
42 lines (37 loc) · 1.3 KB
/
Copy pathscripts.js
File metadata and controls
42 lines (37 loc) · 1.3 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
const lightbox = document.createElement("dialog");
lightbox.className = "image-lightbox";
lightbox.innerHTML = `
<div class="image-lightbox-content">
<button class="lightbox-close" type="button" aria-label="Close image"></button>
<img alt="">
</div>`;
document.body.append(lightbox);
const lightboxImage = lightbox.querySelector("img");
const closeButton = lightbox.querySelector(".lightbox-close");
function openLightbox(thumbnail) {
lightboxImage.src = thumbnail.currentSrc || thumbnail.src;
lightboxImage.alt = thumbnail.alt;
lightbox.showModal();
}
function closeLightbox() {
lightbox.close();
}
document.querySelectorAll(".gallery img").forEach((thumbnail) => {
thumbnail.tabIndex = 0;
thumbnail.setAttribute("role", "button");
thumbnail.setAttribute("aria-label", `${thumbnail.alt}. View full size`);
thumbnail.addEventListener("click", () => openLightbox(thumbnail));
thumbnail.addEventListener("keydown", (event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
openLightbox(thumbnail);
}
});
});
closeButton.addEventListener("click", closeLightbox);
lightbox.addEventListener("click", (event) => {
if (event.target === lightbox) closeLightbox();
});
lightbox.addEventListener("close", () => {
lightboxImage.removeAttribute("src");
});