hic-et-nunc/assets/js/home-slideshow.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

const slideshow = document.getElementById("home-slideshow");
2026-05-26 10:56:17 +02:00
if (slideshow) {
const items = [...slideshow.querySelectorAll(".slide")];
const prev = slideshow.querySelector(".prev");
const next = slideshow.querySelector(".next");
2026-05-26 10:56:17 +02:00
let current = 0;
function isPortrait() {
return window.innerHeight > window.innerWidth;
}
function isSkipped(index) {
const el = items[index];
if (isPortrait() && el.classList.contains("desktop-only")) return true;
if (!isPortrait() && el.classList.contains("mobile-only")) return true;
return false;
}
function findNext(from, direction) {
let index = (from + direction + items.length) % items.length;
let steps = 0;
while (isSkipped(index) && steps < items.length) {
index = (index + direction + items.length) % items.length;
steps++;
}
return index;
}
2026-05-26 10:56:17 +02:00
function goTo(index) {
items[current].classList.remove("active");
current = index;
items[current].classList.add("active");
2026-05-26 10:56:17 +02:00
}
const firstVisible = findNext(-1, 1);
items[firstVisible]?.classList.add("active");
current = firstVisible;
prev?.addEventListener("click", () => goTo(findNext(current, -1)));
next?.addEventListener("click", () => goTo(findNext(current, 1)));
// Recalculer la slide active si on tourne l'écran
window.addEventListener("orientationchange", () => {
if (isSkipped(current)) {
goTo(findNext(current, 1));
}
});
}