45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import GLightbox from "glightbox";
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
if (!document.querySelector("main.product-page")) {
|
|
return;
|
|
}
|
|
|
|
const pictureLinks = document.querySelectorAll(".glightbox") as NodeListOf<HTMLLinkElement>,
|
|
pictures = document.querySelectorAll(".glightbox img") as NodeListOf<HTMLImageElement>,
|
|
imageCount = pictures.length;
|
|
let counter: number = 0,
|
|
glightbox: GLightbox|null = null;
|
|
|
|
function incrementCounter() {
|
|
counter++;
|
|
if ( counter === imageCount ) {
|
|
glightbox = new GLightbox({
|
|
selector: ".glightbox",
|
|
});
|
|
}
|
|
}
|
|
|
|
pictures.forEach((imageElement) => {
|
|
if (imageElement.complete) {
|
|
incrementCounter();
|
|
} else {
|
|
imageElement.addEventListener( "load", incrementCounter, false );
|
|
}
|
|
});
|
|
|
|
pictureLinks.forEach((linkElement, index) => linkElement.addEventListener("keydown", (kbdEvt:KeyboardEvent) => {
|
|
if (
|
|
kbdEvt.key == " " ||
|
|
kbdEvt.code == "Space" ||
|
|
kbdEvt.keyCode == 32 ||
|
|
kbdEvt.key == "Enter" ||
|
|
kbdEvt.code == "Enter" ||
|
|
kbdEvt.keyCode == 13
|
|
) {
|
|
glightbox.openAt(index);
|
|
kbdEvt.preventDefault();
|
|
}
|
|
}));
|
|
});
|