hic-et-nunc/assets/js/home-slideshow.js
Camilledenoray 3486ae9c6c
All checks were successful
Deploy / Deploy to Production (push) Successful in 8s
Home > slideshow > videos verticales incluses dans paires
2026-07-07 20:19:57 +02:00

67 lines
1.9 KiB
JavaScript

const slideshow = document.getElementById("home-slideshow");
if (slideshow) {
const items = [...slideshow.querySelectorAll(".slide")];
const prev = slideshow.querySelector(".prev");
const next = slideshow.querySelector(".next");
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;
}
function updateVideos(index) {
const el = items[index];
const videos = el.querySelectorAll("video");
videos.forEach((video) => {
video.currentTime = 0;
video.play().catch(() => {});
});
}
function pauseAllVideos() {
items.forEach((el) => {
const videos = el.querySelectorAll("video");
videos.forEach((video) => video.pause());
});
}
function goTo(index) {
items[current].classList.remove("active");
pauseAllVideos();
current = index;
items[current].classList.add("active");
updateVideos(current);
}
const firstVisible = findNext(-1, 1);
items[firstVisible]?.classList.add("active");
current = firstVisible;
updateVideos(current);
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));
}
});
}