Webseite-Freund-des-Spessarts/theme/friends-spessart/assets/js/main.js

107 lines
3.5 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function () {
var toggle = document.querySelector('.menu-toggle');
var nav = document.querySelector('.main-nav');
if (!toggle || !nav) return;
toggle.addEventListener('click', function () {
var isOpen = nav.classList.toggle('is-open');
toggle.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
});
});
document.addEventListener('DOMContentLoaded', function () {
var images = document.querySelectorAll('.entry-content img');
if (!images.length) return;
var lightbox = document.createElement('div');
lightbox.className = 'fds-lightbox';
lightbox.setAttribute('role', 'dialog');
lightbox.setAttribute('aria-modal', 'true');
lightbox.setAttribute('aria-label', 'Bild vergrößert');
lightbox.innerHTML = '<button type="button" class="fds-lightbox__close" aria-label="Schließen">&times;</button><img alt="">';
document.body.appendChild(lightbox);
var lightboxImg = lightbox.querySelector('img');
var closeBtn = lightbox.querySelector('.fds-lightbox__close');
var lastFocused = null;
function openLightbox(src, alt) {
lastFocused = document.activeElement;
lightboxImg.src = src;
lightboxImg.alt = alt || '';
lightbox.classList.add('is-open');
closeBtn.focus();
document.body.style.overflow = 'hidden';
}
function closeLightbox() {
lightbox.classList.remove('is-open');
lightboxImg.src = '';
document.body.style.overflow = '';
if (lastFocused) lastFocused.focus();
}
images.forEach(function (img) {
img.classList.add('is-zoomable');
img.setAttribute('tabindex', '0');
img.setAttribute('role', 'button');
img.setAttribute('aria-label', 'Bild vergrößern' + (img.alt ? ': ' + img.alt : ''));
img.addEventListener('click', function () {
openLightbox(img.currentSrc || img.src, img.alt);
});
img.addEventListener('keydown', function (e) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
openLightbox(img.currentSrc || img.src, img.alt);
}
});
});
closeBtn.addEventListener('click', closeLightbox);
lightbox.addEventListener('click', function (e) {
if (e.target === lightbox) closeLightbox();
});
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && lightbox.classList.contains('is-open')) closeLightbox();
});
});
document.addEventListener('DOMContentLoaded', function () {
var forms = document.querySelectorAll('.member-form');
forms.forEach(function (form) {
var tabs = form.querySelectorAll('.member-form__tab');
var panels = form.querySelectorAll('.member-form__panel');
if (!tabs.length || !panels.length) return;
var cf7Form = form.querySelector('form');
if (cf7Form) cf7Form.setAttribute('novalidate', 'novalidate');
function activate(panelNumber) {
tabs.forEach(function (tab) {
tab.classList.toggle('is-active', tab.dataset.panel === String(panelNumber));
});
panels.forEach(function (panel) {
panel.classList.toggle('is-active', panel.dataset.panel === String(panelNumber));
});
}
tabs.forEach(function (tab) {
tab.addEventListener('click', function () {
activate(tab.dataset.panel);
});
});
form.querySelectorAll('[data-goto]').forEach(function (btn) {
btn.addEventListener('click', function () {
activate(btn.dataset.goto);
});
});
form.addEventListener('wpcf7invalid', function () {
var invalidField = form.querySelector('.wpcf7-not-valid');
if (!invalidField) return;
var panel = invalidField.closest('.member-form__panel');
if (panel) activate(panel.dataset.panel);
});
});
});