-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
41 lines (35 loc) · 1.3 KB
/
script.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
// Select elements
const lightbox = document.getElementById('lightbox');
const lightboxImg = document.getElementById('lightbox-img');
const galleryItems = document.querySelectorAll('.gallery-item');
const closeBtn = document.querySelector('.close');
const prevBtn = document.querySelector('.prev');
const nextBtn = document.querySelector('.next');
let currentIndex = 0;
// Open Lightbox
galleryItems.forEach((item, index) => {
item.addEventListener('click', () => {
currentIndex = index; // Track which image is clicked
lightboxImg.src = item.src; // Set the lightbox image source
lightbox.style.display = 'flex'; // Show the lightbox
});
});
// Close Lightbox
closeBtn.addEventListener('click', () => {
lightbox.style.display = 'none';
});
// Navigate Images
prevBtn.addEventListener('click', () => {
currentIndex = (currentIndex > 0) ? currentIndex - 1 : galleryItems.length - 1;
lightboxImg.src = galleryItems[currentIndex].src;
});
nextBtn.addEventListener('click', () => {
currentIndex = (currentIndex < galleryItems.length - 1) ? currentIndex + 1 : 0;
lightboxImg.src = galleryItems[currentIndex].src;
});
// Close Lightbox on Background Click
lightbox.addEventListener('click', (e) => {
if (e.target === lightbox) {
lightbox.style.display = 'none';
}
});