updating chapter indicator heigh depending on relative element

This commit is contained in:
Antoine M 2023-12-04 11:30:27 +01:00
parent 1f6aefe3ed
commit bb00f73621

View File

@ -16,9 +16,12 @@ export default function singleConseil() {
});
targetBlock.focus({ preventScroll: true });
}
function handleChapterIndicatorPosition(targetPosition) {
function handleChapterIndicatorPosition(targetLink) {
const targetPosition = targetLink.offsetTop;
const targetHeight = targetLink.offsetHeight;
let chapterIndicator = document.querySelector('.chapter_index__position-indicator');
chapterIndicator.style.top = targetPosition + 'px';
chapterIndicator.style.height = targetHeight + 'px';
}
let chapterLinks = document.querySelectorAll('.chapter_index__link');
@ -40,9 +43,9 @@ export default function singleConseil() {
if (entry.isIntersecting) {
// Add 'active' class if observation target is inside viewport
handleChapterIndicatorPosition(relatedChapterLink);
entry.target.classList.add('active');
relatedChapterLink.classList.add('active');
handleChapterIndicatorPosition(relatedChapterLink.offsetTop);
} else {
// Remove 'active' class otherwise
// entry.target.classList.remove('active');