From c30136892257f605c171d40b5321e0cc223a2039 Mon Sep 17 00:00:00 2001
From: Your Name <you@example.com>
Date: Mon, 16 Sep 2024 14:22:07 +0000
Subject: [PATCH] Worked around wrong cached positions with firefox
---
assets/js/index.js | 33 ++++++++++++---------------------
1 files changed, 12 insertions(+), 21 deletions(-)
diff --git a/assets/js/index.js b/assets/js/index.js
index 8923fc4..16d0d96 100644
--- a/assets/js/index.js
+++ b/assets/js/index.js
@@ -52,7 +52,7 @@
}
}
-function activeHeading(position, list_links) {
+function activeHeading(index, list_links) {
let links_to_modify = Object.create(null);
links_to_modify.active = list_links.filter(function(link) {
return containsClass(link, active);
@@ -60,9 +60,7 @@
// activeTocLink ? deleteClass
- links_to_modify.new = list_links.filter(function(link){
- return parseInt(link.dataset.position) === position
- })[0];
+ links_to_modify.new = list_links[index]
if (links_to_modify.active != links_to_modify.new) {
links_to_modify.active ? deleteClass(links_to_modify.active, active): false;
@@ -107,26 +105,19 @@
if(current_toc) {
const page_internal_links = Array.from(elems('a', current_toc));
- const page_ids = page_internal_links.map(function(link){
- return link.hash;
- });
-
- const link_positions = page_ids.map(function(id){
- const heading = document.getElementById(decodeURIComponent(id.replace('#','')));
- const position = heading.offsetTop;
- return position;
- });
-
- page_internal_links.forEach(function(link, index){
- link.dataset.position = link_positions[index]
+ const page_links = page_internal_links.map(function(link){
+ return document.getElementById(decodeURIComponent(link.hash.replace('#','')));
});
window.addEventListener('scroll', function(e) {
- // this.setTimeout(function(){
- let position = window.scrollY;
- let active = closestInt(position, link_positions);
- activeHeading(active, page_internal_links);
- // }, 1500)
+ let position = window.scrollY + window.innerHeight/2;
+ let active_index = 0;
+ for (const [index, element] of page_links.entries()) {
+ if(element.offsetTop < position && element.offsetTop > page_links[active_index].offsetTop) {
+ active_index = index;
+ }
+ }
+ activeHeading(active_index, page_internal_links);
});
}
}
--
Gitblit v1.10.0