From 7c2466a21abf41e1feb129ff7d87b616da0b5c1a Mon Sep 17 00:00:00 2001 From: Thomas Chartron Date: Sat, 13 Nov 2021 14:28:56 +0100 Subject: [PATCH] update --- src/js/page.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/js/page.js b/src/js/page.js index eeea0a0..d6569a6 100644 --- a/src/js/page.js +++ b/src/js/page.js @@ -7,23 +7,28 @@ document.addEventListener("DOMContentLoaded", function() { // const navSections = new Array($('.toc').length); window.addEventListener('scroll', activeTocItem) - let has_one_active_toc = false + // let has_one_active_toc = false + let current_selected_toc = null const observer = new window.IntersectionObserver(entries => { entries.forEach(entry => { // Add 'active' class if observation target is inside viewport // console.log(entry.isIntersecting) // console.log(entry.intersectionRatio) - if (entry.isIntersecting && !has_one_active_toc) { + if (entry.isIntersecting) { + // has_one_active_toc = true console.log(entry, 'active') let res = findCorrespondingTocTitle(entry.target) + if (current_selected_toc !== null && current_selected_toc !== res) { + current_selected_toc.classList.remove('bg-blue-800'); + current_selected_toc = res + } console.log(res) res.classList.add('bg-blue-800'); - has_one_active_toc = true } else { + // has_one_active_toc = false console.log(entry, 'inactive') let res = findCorrespondingTocTitle(entry.target) res.classList.remove('bg-blue-800'); - has_one_active_toc = false } }) }, {