-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (40 loc) · 1.71 KB
/
app.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
document.addEventListener('DOMContentLoaded', () => {
const images = document.querySelectorAll('.gallery-image img');
const popup = document.querySelector('.popup');
const largeImage = document.querySelector('.large-image');
const closeBtn = document.querySelector('.close-btn');
const leftArrow = document.querySelector('.left-arrow');
const rightArrow = document.querySelector('.right-arrow');
const imageName = document.querySelector('.image-name');
const imageIndex = document.querySelector('.index');
let currentIndex = 0;
function showImage(index) {
largeImage.src = images[index].src;
imageName.textContent = images[index].getAttribute('src').split('/').pop();
imageIndex.textContent = (index + 1).toString().padStart(2, '0');
currentIndex = index;
}
images.forEach((img, index) => {
img.addEventListener('click', () => {
popup.classList.add('active');
showImage(index);
});
});
closeBtn.addEventListener('click', () => {
popup.classList.remove('active');
});
leftArrow.addEventListener('click', () => {
currentIndex = (currentIndex - 1 + images.length) % images.length;
showImage(currentIndex);
});
rightArrow.addEventListener('click', () => {
currentIndex = (currentIndex + 1) % images.length;
showImage(currentIndex);
});
document.addEventListener('keydown', (e) => {
if (!popup.classList.contains('active')) return;
if (e.key === 'ArrowLeft') leftArrow.click();
if (e.key === 'ArrowRight') rightArrow.click();
if (e.key === 'Escape') closeBtn.click();
});
});