handling current active chapter index in intersection observer

This commit is contained in:
Antoine M 2023-11-27 18:51:51 +01:00
parent 5e6bd9751c
commit f5bb2b672d

View File

@ -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);