69 lines
2.9 KiB
JavaScript
69 lines
2.9 KiB
JavaScript
|
|
// Gallery slideshow: prev/next, dots, arrow keys, autoplay that pauses on hover/focus.
|
||
|
|
// The slides are a scroll-snap strip, so swiping on touch screens works natively.
|
||
|
|
document.querySelectorAll('.slideshow').forEach(show => {
|
||
|
|
const strip = show.querySelector('.slides');
|
||
|
|
const slides = [...show.querySelectorAll('.slide')];
|
||
|
|
const controls = show.querySelector('.slideshow-controls');
|
||
|
|
if (!strip || slides.length < 2) return;
|
||
|
|
|
||
|
|
const dots = [...controls.querySelectorAll('.slide-dots button')];
|
||
|
|
const toggle = controls.querySelector('.slide-toggle');
|
||
|
|
const reducedMotion = matchMedia('(prefers-reduced-motion: reduce)').matches;
|
||
|
|
const intervalMs = 5000;
|
||
|
|
|
||
|
|
let current = 0;
|
||
|
|
let playing = !reducedMotion;
|
||
|
|
let hovered = false;
|
||
|
|
let timer = null;
|
||
|
|
|
||
|
|
controls.hidden = false;
|
||
|
|
|
||
|
|
function goTo(index) {
|
||
|
|
current = (index + slides.length) % slides.length;
|
||
|
|
strip.scrollTo({ left: current * strip.clientWidth, behavior: reducedMotion ? 'auto' : 'smooth' });
|
||
|
|
update();
|
||
|
|
}
|
||
|
|
|
||
|
|
function update() {
|
||
|
|
dots.forEach((d, i) => d.setAttribute('aria-current', i === current ? 'true' : 'false'));
|
||
|
|
toggle.textContent = playing ? '❚❚' : '▶';
|
||
|
|
toggle.setAttribute('aria-label', playing ? toggle.dataset.pause : toggle.dataset.play);
|
||
|
|
strip.setAttribute('aria-live', playing ? 'off' : 'polite');
|
||
|
|
}
|
||
|
|
|
||
|
|
function schedule() {
|
||
|
|
clearInterval(timer);
|
||
|
|
if (playing && !hovered) timer = setInterval(() => goTo(current + 1), intervalMs);
|
||
|
|
}
|
||
|
|
|
||
|
|
controls.querySelector('.slide-prev').addEventListener('click', () => { goTo(current - 1); schedule(); });
|
||
|
|
controls.querySelector('.slide-next').addEventListener('click', () => { goTo(current + 1); schedule(); });
|
||
|
|
dots.forEach((d, i) => d.addEventListener('click', () => { goTo(i); schedule(); }));
|
||
|
|
toggle.addEventListener('click', () => { playing = !playing; update(); schedule(); });
|
||
|
|
|
||
|
|
show.addEventListener('keydown', e => {
|
||
|
|
if (e.key === 'ArrowLeft') { goTo(current - 1); schedule(); }
|
||
|
|
if (e.key === 'ArrowRight') { goTo(current + 1); schedule(); }
|
||
|
|
});
|
||
|
|
|
||
|
|
// pause while the pointer or keyboard focus is inside
|
||
|
|
show.addEventListener('pointerenter', () => { hovered = true; schedule(); });
|
||
|
|
show.addEventListener('pointerleave', () => { hovered = false; schedule(); });
|
||
|
|
show.addEventListener('focusin', () => { hovered = true; schedule(); });
|
||
|
|
show.addEventListener('focusout', e => { if (!show.contains(e.relatedTarget)) { hovered = false; schedule(); } });
|
||
|
|
|
||
|
|
// keep the dots in sync when the user swipes or scrolls the strip
|
||
|
|
let scrollEnd;
|
||
|
|
strip.addEventListener('scroll', () => {
|
||
|
|
clearTimeout(scrollEnd);
|
||
|
|
scrollEnd = setTimeout(() => {
|
||
|
|
current = Math.round(strip.scrollLeft / strip.clientWidth);
|
||
|
|
update();
|
||
|
|
}, 100);
|
||
|
|
});
|
||
|
|
addEventListener('resize', () => strip.scrollTo({ left: current * strip.clientWidth }));
|
||
|
|
|
||
|
|
update();
|
||
|
|
schedule();
|
||
|
|
});
|