Make the nav sidebar's scroll-to-current-page behaviour less jumpy

This avoids scrolling the sidebar when the link is already in view by
using "nearest" for the vertical axis ("block") instead of the default
of "start".  This behaviour is much better when clicking on items in the
current viewport, especially if the current viewport matches the initial
viewport.

It's still a little jumpy when the current link isn't visible in the
initial viewport, because the sidebar position still changes across page
loads.  But the new behaviour solves the big problem on initial home
page loads with a long sidebar where the logo and project title would be
immediately scrolled out of view.

Related to #824.
This commit is contained in:
Thomas Sibley 2022-03-29 16:30:50 -07:00
parent 0da22b885b
commit b8535aee8f
2 changed files with 10 additions and 2 deletions

File diff suppressed because one or more lines are too long

View File

@ -159,7 +159,15 @@ function ThemeNav () {
.addClass('current')
.attr('aria-expanded','true');
}
link[0].scrollIntoView();
/* Scroll the link's top-level parent into view first. Then
* scroll the link itself into view, which will only have an
* effect if the top-level parent is taller than the viewport
* and the link is still outside the viewport after the first
* scroll. (This seems likely to be rare.)
*/
link.closest('li.toctree-l1')[0]
.scrollIntoView({block: "nearest"});
link[0].scrollIntoView({block: "nearest"});
}
}
catch (err) {