Skip to content

Commit

Permalink
Simplify offset calculation for sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
choldgraf committed Jan 3, 2022
1 parent c8955eb commit 1f77274
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/pydata_sphinx_theme/assets/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,19 @@ function scrollToActive() {
);

if (!isNaN(storedScrollTop)) {
// If we've got a saved scroll position, just use that
sidebar.scrollTop = storedScrollTop;
} else {
// Otherwise, calculate a position to scroll to based on the lowest `active` link
var active_pages = sidebarNav.querySelectorAll(".active");
var offset = 0;
var i;
for (i = active_pages.length - 1; i > 0; i--) {
var active_page = active_pages[i];
if (active_page !== undefined) {
offset += active_page.offsetTop;
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;
// 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;
}
}
offset -= sidebar.offsetTop;

// Only scroll the navbar if the active link is lower than 50% of the page
if (active_page !== undefined && offset > sidebar.clientHeight * 0.5) {
sidebar.scrollTop = offset - sidebar.clientHeight * 0.2;
}
}

// Store the sidebar scroll position
Expand Down

0 comments on commit 1f77274

Please sign in to comment.