Skip to content

Commit

Permalink
HTML: better scrolling of Table of Contents on selection (PR PreTeXtB…
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarlevin authored and rbeezer committed Nov 2, 2024
1 parent 9700d20 commit ef2db6e
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions js/pretext.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ function scrollTocToActive() {
// Don't use scrollIntoView because it changes users tab position in Chrome
// and messes up keyboard navigation
tocEntry.closest("li").classList.add("active");
document.querySelector("#ptx-toc").scrollTop = tocEntry.offsetTop;
// Scroll only if the tocEntry is below the bottom half of the window,
// scrolling to that position.
document.querySelector("#ptx-toc").scrollTop = tocEntry.offsetTop - 0.4 * self.innerHeight;
}

function toggletoc() {
Expand All @@ -51,12 +53,12 @@ function toggletoc() {
scrollTocToActive();
}

window.addEventListener("load",function(event) {
window.addEventListener("DOMContentLoaded",function(event) {
thetocbutton = document.getElementsByClassName("toc-toggle")[0];
thetocbutton.addEventListener('click', () => toggletoc() );
});

window.addEventListener("load",function(event) {
window.addEventListener("DOMContentLoaded",function(event) {
scrollTocToActive();
});

Expand Down

0 comments on commit ef2db6e

Please sign in to comment.