-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLB.js
More file actions
71 lines (57 loc) · 1.72 KB
/
Copy pathLB.js
File metadata and controls
71 lines (57 loc) · 1.72 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
const images = document.querySelectorAll('.gallery img');
const lightbox = document.getElementById('lightbox');
const lightboxImg = document.querySelector('.lightbox-img');
const prevBtn = document.querySelector('.prev');
const nextBtn = document.querySelector('.next');
let currentGroup = [];
let currentIndex = 0;
// Open lightbox
images.forEach(img => {
img.addEventListener('click', () => {
const group = img.dataset.group;
currentGroup = [...document.querySelectorAll(`img[data-group="${group}"]`)];
currentIndex = currentGroup.indexOf(img);
showImage();
lightbox.classList.add('active');
});
});
function showImage() {
lightboxImg.style.opacity = 0;
const img = new Image();
img.src = currentGroup[currentIndex].dataset.full;
img.onload = () => {
lightboxImg.src = img.src;
lightboxImg.style.opacity = 1;
};
}
// Navigation
prevBtn.addEventListener('click', e => {
e.stopPropagation();
currentIndex = (currentIndex - 1 + currentGroup.length) % currentGroup.length;
showImage();
});
nextBtn.addEventListener('click', e => {
e.stopPropagation();
currentIndex = (currentIndex + 1) % currentGroup.length;
showImage();
});
document.addEventListener('keydown', (e) => {
if (!lightbox.classList.contains('active')) return;
switch (e.key) {
case 'Escape':
lightbox.classList.remove('active');
break;
case 'ArrowLeft':
currentIndex = (currentIndex - 1 + currentGroup.length) % currentGroup.length;
showImage();
break;
case 'ArrowRight':
currentIndex = (currentIndex + 1) % currentGroup.length;
showImage();
break;
}
});
// Close on click outside
lightbox.addEventListener('click', () => {
lightbox.classList.remove('active');
});