-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphoto.js
More file actions
80 lines (67 loc) · 2.56 KB
/
photo.js
File metadata and controls
80 lines (67 loc) · 2.56 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
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
document.addEventListener("DOMContentLoaded", function () {
const lightbox = document.getElementById("lightbox");
const lightboxImg = document.getElementById("lightbox-img");
const closeBtn = document.querySelector(".close");
const prevBtn = document.querySelector(".prev");
const nextBtn = document.querySelector(".next");
const dotsContainer = document.querySelector(".dots-container");
const galleryImages = document.querySelectorAll(".gallery-container img");
let currentIndex = 0;
// Function to open the lightbox
function openLightbox(index) {
currentIndex = index;
lightbox.style.display = "flex";
lightboxImg.src = galleryImages[currentIndex].getAttribute("src");
updateDots();
}
// Add event listener to all gallery images
galleryImages.forEach((img, index) => {
img.addEventListener("click", () => {
openLightbox(index);
});
});
// Function to close the lightbox
closeBtn.addEventListener("click", () => {
lightbox.style.display = "none";
});
// Function to navigate next
nextBtn.addEventListener("click", () => {
currentIndex = (currentIndex + 1) % galleryImages.length;
openLightbox(currentIndex);
});
// Function to navigate previous
prevBtn.addEventListener("click", () => {
currentIndex = (currentIndex - 1 + galleryImages.length) % galleryImages.length;
openLightbox(currentIndex);
});
// Close lightbox when clicking outside the image
lightbox.addEventListener("click", (event) => {
if (event.target === lightbox) {
lightbox.style.display = "none";
}
});
// Create dots for each image
function createDots() {
dotsContainer.innerHTML = "";
galleryImages.forEach((_, index) => {
let dot = document.createElement("span");
dot.classList.add("dot");
if (index === currentIndex) {
dot.classList.add("active");
}
dot.addEventListener("click", () => {
openLightbox(index);
});
dotsContainer.appendChild(dot);
});
}
// Function to update dot indicators
function updateDots() {
let dots = document.querySelectorAll(".dot");
dots.forEach((dot, index) => {
dot.classList.toggle("active", index === currentIndex);
});
}
// Initialize dots on page load
createDots();
});