From a8cff0632786012212e54a5971507e3611957460 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Wed, 10 Jan 2024 14:07:36 +0100 Subject: [PATCH] feat(carousel): javascript addon to manage slides navigation --- assets/scripts/carousel.js | 57 ++++++++++++++++++++++++++++++++++++-- layouts/index.html | 11 ++++---- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/assets/scripts/carousel.js b/assets/scripts/carousel.js index 028425f..4c8086f 100644 --- a/assets/scripts/carousel.js +++ b/assets/scripts/carousel.js @@ -12,16 +12,20 @@ function isElementInViewport (el) { // little addon to enhance the carousel +var carousels = document.querySelectorAll('.carousel'); -Array.from(document.getElementsByClassName("carousel")).forEach((carousel) => { +// first addon to change "enabled" slide indicator (way to complicated) +Array.from(carousels).forEach((carousel) => { let viewport = carousel.getElementsByClassName("carousel__viewport")[0]; let slides = carousel.getElementsByClassName("carousel__slide"); let nav_items = Array.from(carousel.getElementsByClassName("carousel__navigation-item")) let last_scroll_pos = null; viewport.onscroll = (_event) => { - let position = slides.length * viewport.scrollLeft / viewport.scrollWidth; + let position = Math.ceil(slides.length * viewport.scrollLeft / viewport.scrollWidth); let direction = last_scroll_pos == null || last_scroll_pos < viewport.scrollLeft; + console.log("direction", direction); + console.log("position", position); last_scroll_pos = viewport.scrollLeft; nav_items.forEach((item, index) => { @@ -41,3 +45,52 @@ Array.from(document.getElementsByClassName("carousel")).forEach((carousel) => { } }) + +// second addon to replace the anchor with javascript managed jump scroll + +// Iterate through each 'carousel__viewport' element +carousels.forEach(function(carousel) { + var viewport = carousel.querySelector('.carousel__viewport') + + // Get all elements with the class 'carousel__slide' within the current 'carousel__viewport' + var slides = viewport.querySelectorAll('.carousel__slide'); + + var prevButtons = viewport.querySelectorAll('.carousel__prev'); + var nextButtons = viewport.querySelectorAll('.carousel__next'); + var navigationButtons = carousel.querySelectorAll('.carousel__navigation-button'); + console.log(prevButtons) + console.log(navigationButtons) + + prevButtons.forEach(function(prevButton, index) { + // remove href, for css only carousel + prevButton.removeAttribute('href'); + prevButton.addEventListener('click', function() { + console.log("Click of previous", index) + + if (index == 0) { + viewport.scrollLeft = slides.length * slides[0].offsetWidth; + return; + } + viewport.scrollLeft -= slides[0].offsetWidth; + }); + }); + + nextButtons.forEach(function(nextButton, index) { + nextButton.removeAttribute('href'); + nextButton.addEventListener('click', function() { + if (index == slides.length-1) { + viewport.scrollLeft = 0; + return; + } + viewport.scrollLeft += slides[0].offsetWidth; + }); + }); + + navigationButtons.forEach(function(btn, index) { + btn.removeAttribute('href'); + btn.addEventListener('click', function() { + viewport.scrollLeft = slides[0].offsetWidth * index; + }); + }); +}); + diff --git a/layouts/index.html b/layouts/index.html index 2f52d6b..7759802 100644 --- a/layouts/index.html +++ b/layouts/index.html @@ -2,7 +2,7 @@
@@ -45,7 +45,7 @@ - - - -