-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
57 lines (46 loc) · 1.54 KB
/
Copy pathscript.js
File metadata and controls
57 lines (46 loc) · 1.54 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
// Select all gallery images
const galleryImages = document.querySelectorAll('.gallery img');
// Select the modal elements
const modal = document.getElementById('imageModal');
const modalImage = document.getElementById('modalImage');
const closeModal = document.querySelector('.modal .close');
// Arrows for navigation
const prev = document.getElementById('prev');
const next = document.getElementById('next');
// Track the current image index
let currentIndex = 0;
// Open modal when an image is clicked
galleryImages.forEach((img, index) => {
img.addEventListener('click', () => {
currentIndex = index; // Set the current index
openModal(img.src);
});
});
// Function to open modal
function openModal(src) {
modal.style.display = 'flex';
modalImage.src = src;
}
// Close modal when clicking the "x" button
closeModal.addEventListener('click', () => {
modal.style.display = 'none';
});
// Close modal when clicking outside the image
modal.addEventListener('click', (e) => {
if (e.target === modal) {
modal.style.display = 'none';
}
});
// Navigate to the previous image
prev.addEventListener('click', () => {
currentIndex = (currentIndex - 1 + galleryImages.length) % galleryImages.length; // Loop back to last image
modalImage.src = galleryImages[currentIndex].src;
});
// Navigate to the next image
next.addEventListener('click', () => {
currentIndex = (currentIndex + 1) % galleryImages.length; // Loop back to the first image
modalImage.src = galleryImages[currentIndex].src;
});
function goBack() {
window.history.back();
}