From f5bb2b672dea080b1520d2ecc9ad9896fee40b3f Mon Sep 17 00:00:00 2001 From: Antoine M Date: Mon, 27 Nov 2023 18:51:51 +0100 Subject: [PATCH] handling current active chapter index in intersection observer --- resources/js/single-conseil.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/resources/js/single-conseil.js b/resources/js/single-conseil.js index cee9e92..944ebb6 100644 --- a/resources/js/single-conseil.js +++ b/resources/js/single-conseil.js @@ -34,11 +34,14 @@ export default function singleConseil() { const chapterProgressionObserver = new IntersectionObserver( (entries) => { entries.forEach((entry) => { + const blockId = entry.target.getAttribute('id'); + const relatedChapterLink = document.querySelector(`a[href="#${blockId}"]`); + relatedChapterLink.classList.remove('active'); + if (entry.isIntersecting) { // Add 'active' class if observation target is inside viewport entry.target.classList.add('active'); - const blockId = entry.target.getAttribute('id'); - const relatedChapterLink = document.querySelector(`a[href="#${blockId}"]`); + relatedChapterLink.classList.add('active'); handleChapterIndicatorPosition(relatedChapterLink.offsetTop); } else { // Remove 'active' class otherwise @@ -53,11 +56,13 @@ export default function singleConseil() { function buildAllBlocksToObserve() { const questionsContainerBlocks = document.querySelectorAll('.questions-container-block'); + const chapitresThematiques = document.querySelectorAll('.homegrade-blocks-chapitre-thematique'); const vocabulaireSummaryBlock = document.querySelector('.homegrade-blocks-vocabulaire-summary'); const plusLoinBlock = document.querySelector('#aller-plus-loin'); let allBlocks = []; if (questionsContainerBlocks) allBlocks = [...questionsContainerBlocks]; + if (chapitresThematiques) allBlocks = [...chapitresThematiques]; if (vocabulaireSummaryBlock) allBlocks.push(vocabulaireSummaryBlock); if (plusLoinBlock) allBlocks.push(plusLoinBlock);