This commit is contained in:
Thomas Chartron 2021-11-13 14:16:51 +01:00
parent 9baf49d870
commit 7b29e47ba9
2 changed files with 3 additions and 1 deletions

View file

@ -10,6 +10,8 @@ document.addEventListener("DOMContentLoaded", function() {
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.intersectionRatio > 0) {
console.log(entry, 'active')
let res = findCorrespondingTocTitle(entry.target)

View file

@ -1 +1 @@
function activeTocItem(){var e=window.scrollY;console.log(e)}function findCorrespondingTocTitle(o){return[...document.querySelectorAll("#toc li a")].find(e=>(console.log(e),console.log("#"+o.id),console.log(e.href.substring(e.href.indexOf("#"))),e.href.substring(e.href.indexOf("#"))==="#"+o.id))}document.addEventListener("DOMContentLoaded",function(){document.getElementById("navbar").clientHeight,document.getElementById("toc");var e=document.querySelectorAll("#toc li a");document.getElementById("page-content");window.addEventListener("scroll",activeTocItem);const o=new window.IntersectionObserver(e=>{e.forEach(o=>{if(0<o.intersectionRatio){console.log(o,"active");let e=findCorrespondingTocTitle(o.target);console.log(e),e.classList.add("bg-blue-800")}else console.log(o,"inactive"),o.target.classList.remove("bg-blue-800")})},{root:null,threshold:.1});var n=[];[...e].forEach(e=>{n.push(e.href.substring(e.href.indexOf("#")))});const t=document.querySelectorAll(n.join(","));t.forEach(e=>{o.observe(e)})});
function activeTocItem(){var e=window.scrollY;console.log(e)}function findCorrespondingTocTitle(o){return[...document.querySelectorAll("#toc li a")].find(e=>(console.log(e),console.log("#"+o.id),console.log(e.href.substring(e.href.indexOf("#"))),e.href.substring(e.href.indexOf("#"))==="#"+o.id))}document.addEventListener("DOMContentLoaded",function(){document.getElementById("navbar").clientHeight,document.getElementById("toc");var e=document.querySelectorAll("#toc li a");document.getElementById("page-content");window.addEventListener("scroll",activeTocItem);const o=new window.IntersectionObserver(e=>{e.forEach(o=>{if(console.log(o.isIntersecting),console.log(o.intersectionRatio),0<o.intersectionRatio){console.log(o,"active");let e=findCorrespondingTocTitle(o.target);console.log(e),e.classList.add("bg-blue-800")}else console.log(o,"inactive"),o.target.classList.remove("bg-blue-800")})},{root:null,threshold:.1});var n=[];[...e].forEach(e=>{n.push(e.href.substring(e.href.indexOf("#")))});const t=document.querySelectorAll(n.join(","));t.forEach(e=>{o.observe(e)})});