From 5a5edd034242e7711d9939c02a9b96b07f921d50 Mon Sep 17 00:00:00 2001 From: Thomas Chartron Date: Sat, 13 Nov 2021 17:25:38 +0100 Subject: [PATCH] update --- src/js/page.js | 55 +++++++++++++++++++++++++++++------------------ static/js/page.js | 2 +- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/js/page.js b/src/js/page.js index 78326d4..4d2e323 100644 --- a/src/js/page.js +++ b/src/js/page.js @@ -12,31 +12,44 @@ document.addEventListener("DOMContentLoaded", function() { let current_intersectiong_entry = null const observer = new window.IntersectionObserver(entries => { entries.some(entry => { + console.log('before') + console.log('entry', entry) + // console.log('current', current_intersectiong_entry) + + if (entry.isIntersecting) { + let res = findCorrespondingTocTitle(entry.target) + res.parentElement.classList.add('bg-blue-800'); + // current_intersectiong_entry = entry + return true; + } + if (!entry.isIntersecting) { + let res = findCorrespondingTocTitle(entry.target) //First intersection entry + res.parentElement.classList.remove('bg-blue-800'); + res.parentElement.nextElementSibling.classList.add('bg-blue-800'); + return true; + } // console.log('observe') // Add 'active' class if observation target is inside viewport // console.log(entry) // console.log(entry.intersectionRatio) - console.log('before') - console.log('entry', entry) - console.log('current', current_intersectiong_entry) - if (entry.isIntersecting) { - if (current_intersectiong_entry === null) { // first page load - let res = findCorrespondingTocTitle(entry.target) - res.parentElement.classList.add('bg-blue-800'); - current_intersectiong_entry = entry - return true; - } - if (current_intersectiong_entry.boundingClientRect.y < 0) { //previous selected is out of viewport - current_intersectiong_entry.target.classList.remove('bg-blue-800'); - let res = findCorrespondingTocTitle(entry.target) //First intersection entry - res.parentElement.classList.add('bg-blue-800'); - current_intersectiong_entry = entry - return true; - } - } - console.log('after') - console.log('entry', entry) - console.log('current', current_intersectiong_entry) + // if (entry.isIntersecting) { + // if (current_intersectiong_entry === null) { // first page load + // let res = findCorrespondingTocTitle(entry.target) + // res.parentElement.classList.add('bg-blue-800'); + // current_intersectiong_entry = entry + // return true; + // } + // if (current_intersectiong_entry.boundingClientRect.y < 0) { //previous selected is out of viewport + // current_intersectiong_entry.target.classList.remove('bg-blue-800'); + // let res = findCorrespondingTocTitle(entry.target) //First intersection entry + // res.parentElement.classList.add('bg-blue-800'); + // current_intersectiong_entry = entry + // return true; + // } + // } + // console.log('after') + // console.log('entry', entry) + // console.log('current', current_intersectiong_entry) // if (entry.isIntersecting === false) { //some section got out of viewport remove its active class and add to next sibling // let res = findCorrespondingTocTitle(entry.target) //First intersection entry // res.parentElement.classList.remove('bg-blue-800'); diff --git a/static/js/page.js b/static/js/page.js index cac0a03..154f834 100644 --- a/static/js/page.js +++ b/static/js/page.js @@ -1 +1 @@ -function findCorrespondingTocTitle(n){return[...document.querySelectorAll("#toc li a")].find(e=>e.href.substring(e.href.indexOf("#"))==="#"+n.id)}document.addEventListener("DOMContentLoaded",function(){document.getElementById("navbar").clientHeight,document.getElementById("toc");var e=document.querySelectorAll("#toc li a");document.getElementById("page-content");let t=null;const n=new window.IntersectionObserver(e=>{e.some(n=>{if(console.log("before"),console.log("entry",n),console.log("current",t),n.isIntersecting){if(null===t){let e=findCorrespondingTocTitle(n.target);return e.parentElement.classList.add("bg-blue-800"),t=n,!0}if(t.boundingClientRect.y<0){t.target.classList.remove("bg-blue-800");let e=findCorrespondingTocTitle(n.target);return e.parentElement.classList.add("bg-blue-800"),t=n,!0}}console.log("after"),console.log("entry",n),console.log("current",t)})},{root:null,threshold:.1});var o=[];[...e].forEach(e=>{o.push(e.href.substring(e.href.indexOf("#")))});const r=document.querySelectorAll(o.join(","));r.forEach(e=>{n.observe(e)})}); \ No newline at end of file +function findCorrespondingTocTitle(t){return[...document.querySelectorAll("#toc li a")].find(e=>e.href.substring(e.href.indexOf("#"))==="#"+t.id)}document.addEventListener("DOMContentLoaded",function(){document.getElementById("navbar").clientHeight,document.getElementById("toc");var e=document.querySelectorAll("#toc li a");document.getElementById("page-content");const t=new window.IntersectionObserver(e=>{e.some(t=>{if(console.log("before"),console.log("entry",t),t.isIntersecting){let e=findCorrespondingTocTitle(t.target);return e.parentElement.classList.add("bg-blue-800"),!0}if(!t.isIntersecting){let e=findCorrespondingTocTitle(t.target);return e.parentElement.classList.remove("bg-blue-800"),e.parentElement.nextElementSibling.classList.add("bg-blue-800"),!0}})},{root:null,threshold:.1});var n=[];[...e].forEach(e=>{n.push(e.href.substring(e.href.indexOf("#")))});const o=document.querySelectorAll(n.join(","));o.forEach(e=>{t.observe(e)})}); \ No newline at end of file