Skip to content

Commit b8535ae

Browse files
committed
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.
1 parent 0da22b8 commit b8535ae

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

sphinx_rtd_theme/static/js/theme.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/theme.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,15 @@ function ThemeNav () {
159159
.addClass('current')
160160
.attr('aria-expanded','true');
161161
}
162-
link[0].scrollIntoView();
162+
/* Scroll the link's top-level parent into view first. Then
163+
* scroll the link itself into view, which will only have an
164+
* effect if the top-level parent is taller than the viewport
165+
* and the link is still outside the viewport after the first
166+
* scroll. (This seems likely to be rare.)
167+
*/
168+
link.closest('li.toctree-l1')[0]
169+
.scrollIntoView({block: "nearest"});
170+
link[0].scrollIntoView({block: "nearest"});
163171
}
164172
}
165173
catch (err) {

0 commit comments

Comments
 (0)