Skip to content

Commit

Permalink
Clientrect for sidebar scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
choldgraf committed Jan 11, 2022
1 parent 1f77274 commit 8ef7370
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/pydata_sphinx_theme/assets/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,20 @@ function scrollToActive() {
if (!isNaN(storedScrollTop)) {
// If we've got a saved scroll position, just use that
sidebar.scrollTop = storedScrollTop;
console.log("[PST]: Scrolled sidebar using stored browser position...");
} else {
// Otherwise, calculate a position to scroll to based on the lowest `active` link
var active_pages = sidebarNav.querySelectorAll(".active");
if (active_pages.length > 0) {
// Use the last active page as the offset since it's the page we're on
var offset = active_pages[active_pages.length - 1].offsetTop;
var latest_active = active_pages[active_pages.length - 1];
var offset =
latest_active.getBoundingClientRect().y -
sidebar.getBoundingClientRect().y;
// Only scroll the navbar if the active link is lower than 50% of the page
if (offset > sidebar.clientHeight * 0.5) {
sidebar.scrollTop = offset - sidebar.clientHeight * 0.2;
console.log("[PST]: Scrolled sidebar using last active link...");
}
}
}
Expand Down

0 comments on commit 8ef7370

Please sign in to comment.