Skip to content

Commit aff50d9

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 aff50d9

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/theme.js

+9-1
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)