Skip to content

Commit 6b9242d

Browse files
committed
auto-scroll active nav item view
1 parent 4d4f9e8 commit 6b9242d

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

static/scroll-active-nav.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Scroll the active navigation item into view on page load
3+
*/
4+
(function() {
5+
'use strict';
6+
7+
function scrollActiveNavIntoView() {
8+
// Find the active navigation item in the sidebar
9+
const activeNavItem = document.querySelector('.site-navigation__entry.active');
10+
11+
if (activeNavItem) {
12+
// Scroll it into view with some padding
13+
activeNavItem.scrollIntoView({
14+
behavior: 'auto',
15+
block: 'center',
16+
inline: 'nearest'
17+
});
18+
}
19+
}
20+
21+
// Run when DOM is ready
22+
if (document.readyState === 'loading') {
23+
document.addEventListener('DOMContentLoaded', scrollActiveNavIntoView);
24+
} else {
25+
// DOM is already ready
26+
scrollActiveNavIntoView();
27+
}
28+
})();

templates/base.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<script type="text/javascript" src="{{ get_url(path='mobile-menu.js') }}"></script>
4646
<script type="text/javascript" src="{{ get_url(path='dark-mode.js') }}"></script>
4747
<script type="text/javascript" src="{{ get_url(path='sidebar-toggle.js') }}"></script>
48+
<script type="text/javascript" src="{{ get_url(path='scroll-active-nav.js') }}"></script>
4849
<script type="text/javascript" src="{{ get_url(path='copy-code.js') }}" defer></script>
4950
<script type="text/javascript" src="{{ get_url(path='elasticlunr.min.js') }}" defer></script>
5051
<script type="text/javascript" src="{{ get_url(path='api_search.js') }}" defer></script>

0 commit comments

Comments
 (0)