Skip to content

Commit

Permalink
trying to resume scroll position
Browse files Browse the repository at this point in the history
  • Loading branch information
SichangHe committed Oct 24, 2024
1 parent 9efca6b commit bbacc8b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions theme/js/math.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const sidebar_toc = document.querySelector("#sidebar > div.toc");
const storageKey = "lastY";
let lastY = localStorage.getItem(storageKey) || 0;

(function() {
const sidebar = document.querySelector("#sidebar > div.sidebar-scrollbox:not(.toc)");
Expand All @@ -17,6 +19,12 @@ const sidebar_toc = document.querySelector("#sidebar > div.toc");
});
})();

function resumeScroll() {
if (lastY) {
window.scrollTo(0, lastY);
}
}

function fix_toc_n_add_math_copying() {
const toc = document.querySelector("#content > main > ul");
if (!toc) {
Expand Down Expand Up @@ -61,8 +69,20 @@ function fix_toc_n_add_math_copying() {
() => navigator.clipboard.writeText(data.value),
);
}
resumeScroll();
return true;
}

function saveScroll() {
const diff = Math.abs(lastY - window.scrollY);
if (diff > 100) {
localStorage.setItem(storageKey, window.scrollY);
lastY = window.scrollY;
}
}

if (!fix_toc_n_add_math_copying()) {
document.addEventListener("DOMContentLoaded", fix_toc_n_add_math_copying);
}
document.addEventListener("load", resumeScroll);
document.addEventListener("scrollend", saveScroll);

0 comments on commit bbacc8b

Please sign in to comment.