LuminaWebsite/assets/js/lightbox.js
2026-09-21 20:45:37 +02:00

77 lines
2.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Einfache Grossansicht fuer Bilder: Links mit data-lightbox="gruppe" oeffnen das Bild in
* einem Overlay, mit Pfeilen / Wischen durch die Bilder derselben Gruppe blaettern.
*/
(function () {
'use strict';
var links = Array.prototype.slice.call(document.querySelectorAll('a[data-lightbox]'));
if (!links.length) return;
var overlay = document.createElement('div');
overlay.className = 'lightbox';
overlay.hidden = true;
overlay.innerHTML =
'<button type="button" class="lightbox-close" aria-label="Schließen">✕</button>' +
'<button type="button" class="lightbox-prev" aria-label="Vorheriges Bild">‹</button>' +
'<figure><img alt=""><figcaption></figcaption></figure>' +
'<button type="button" class="lightbox-next" aria-label="Nächstes Bild">›</button>';
document.body.appendChild(overlay);
var img = overlay.querySelector('img');
var caption = overlay.querySelector('figcaption');
var group = [];
var index = 0;
var touchX = null;
function show(i) {
index = (i + group.length) % group.length;
var link = group[index];
img.src = link.href;
img.alt = link.dataset.caption || '';
caption.textContent = link.dataset.caption || '';
overlay.querySelector('.lightbox-prev').hidden = group.length < 2;
overlay.querySelector('.lightbox-next').hidden = group.length < 2;
}
function open(link) {
group = links.filter(function (l) { return l.dataset.lightbox === link.dataset.lightbox; });
overlay.hidden = false;
document.body.style.overflow = 'hidden';
show(group.indexOf(link));
}
function close() {
overlay.hidden = true;
document.body.style.overflow = '';
img.src = '';
}
links.forEach(function (link) {
link.addEventListener('click', function (e) {
e.preventDefault();
open(link);
});
});
overlay.addEventListener('click', function (e) {
if (e.target === overlay || e.target.classList.contains('lightbox-close')) close();
else if (e.target.classList.contains('lightbox-prev')) show(index - 1);
else if (e.target.classList.contains('lightbox-next')) show(index + 1);
});
document.addEventListener('keydown', function (e) {
if (overlay.hidden) return;
if (e.key === 'Escape') close();
else if (e.key === 'ArrowLeft') show(index - 1);
else if (e.key === 'ArrowRight') show(index + 1);
});
overlay.addEventListener('touchstart', function (e) { touchX = e.touches[0].clientX; }, { passive: true });
overlay.addEventListener('touchend', function (e) {
if (touchX === null || group.length < 2) return;
var dx = e.changedTouches[0].clientX - touchX;
if (Math.abs(dx) > 50) show(index + (dx < 0 ? 1 : -1));
touchX = null;
});
})();