Repository navigation
Expand file tree
/
Copy pathgallery.js
More file actions
41 lines (36 loc) · 1.4 KB
/
Copy pathgallery.js
File metadata and controls
41 lines (36 loc) · 1.4 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
// Gallery lightbox
let current = 0;
let images = [];
document.querySelectorAll('.gallery-thumb').forEach((img, i) => {
images.push({ src: img.src, caption: img.dataset.caption || '' });
img.setAttribute('tabindex', '0');
img.setAttribute('role', 'button');
img.addEventListener('click', () => openLightbox(i));
img.addEventListener('keydown', e => {
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); openLightbox(i); }
});
});
function openLightbox(i) {
current = i;
const lb = document.getElementById('lightbox');
document.getElementById('lightbox-img').src = images[i].src;
document.getElementById('lightbox-caption').textContent = images[i].caption;
lb.classList.add('active');
document.body.style.overflow = 'hidden';
}
function closeLightbox() {
document.getElementById('lightbox').classList.remove('active');
document.body.style.overflow = '';
}
function shiftLightbox(dir) {
current = (current + dir + images.length) % images.length;
document.getElementById('lightbox-img').src = images[current].src;
document.getElementById('lightbox-caption').textContent = images[current].caption;
}
document.addEventListener('keydown', e => {
const lb = document.getElementById('lightbox');
if (!lb.classList.contains('active')) return;
if (e.key === 'Escape') closeLightbox();
if (e.key === 'ArrowLeft') shiftLightbox(-1);
if (e.key === 'ArrowRight') shiftLightbox(1);
});