-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodal.js
83 lines (68 loc) · 2.45 KB
/
modal.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
// Get the modal and close button
var modal = document.getElementById("gallery-modal");
var closeButton = document.getElementsByClassName("close")[0];
// Next & previous buttons in the modal
var nextButton = document.getElementById("next-button");
var prevButton = document.getElementById("prev-button");
// Get all the image links in the gallery
var imageLinks = Array.from(document.getElementsByClassName("photo-gallery-image"))
// Variables to track the currently displayed image and total number of images
var currentImageIndex = 0;
var totalImages = imageLinks.length;
// Function to display the current image
function displayImage(index) {
// Get the larger image URL from the data attribute
var imageURL = imageLinks[index].getAttribute("src");
// Update the modal image source
var modalImage = document.getElementById("modal-image");
modalImage.src = imageURL;
}
function openModal() {
modal.style.display = "block";
displayImage(currentImageIndex);
}
function closeModal() {
modal.style.display = "none";
}
function nextImage() {
currentImageIndex = (currentImageIndex + 1) % totalImages;
displayImage(currentImageIndex);
}
function previousImage() {
currentImageIndex = (currentImageIndex - 1 + totalImages) % totalImages;
displayImage(currentImageIndex);
}
// Add click event listeners to each image link
imageLinks.forEach(function (link, index) {
link.addEventListener("click", function () {
currentImageIndex = index;
openModal();
});
});
// Close the modal when the close button is clicked
closeButton.addEventListener("click", closeModal);
// Close the modal when the user clicks outside of it
modal.addEventListener("click", function (event) {
if ("className" in event.target && event.target.className === "modal-content") {
closeModal();
}
});
// Add keydown event listener for arrow keys for modal navigation, and escape for closing it
document.addEventListener("keydown", function (event) {
if (event.key === "ArrowLeft") {
previousImage();
} else if (event.key === "ArrowRight") {
nextImage();
} else if (event.key === "Escape") {
closeModal();
}
});
// Also add modal navigation via button cick
nextButton.addEventListener("click", function () {
currentImageIndex = (currentImageIndex + 1) % totalImages;
displayImage(currentImageIndex);
});
prevButton.addEventListener("click", function () {
currentImageIndex = (currentImageIndex - 1 + totalImages) % totalImages;
displayImage(currentImageIndex);
});