Page navigation via anchor links, i.e. when you click a menu link in the nav
, you're taken to the corresponding section in the page. The nav
has position: sticky
, so the page content moves under it on page scroll. A shadow at the base of the nav
appears on scroll, as well as a 'Back to top' link.
Clicking a link or scrolling to a section will highlight the corresponding menu link, and briefly underline the associated section
heading.
The JavaScript achieves 3 things:
- It adds the shadow on the base of the
nav
and the 'Back to top' link to the bottom of the page, on scroll. The shadow and link are removed if you scroll back to the top of the page. - It adds a highlight to menu links and an underline on the
section
heading, (on click or on scroll) . - It dynamically calculates the height of the
nav
as the viewport narrows or expands. This means you don't have to set a fixed height on thenav
. The resulting value provides the offset of the main content. Without this offset the page anchor target content would be hidden under thenav
.
- ES6 Modules (no transpilation to ES5)
js/modules/observers/article-header-intersection-observer
adds / removes thenav
shadow and the 'Back to top' link on scroll.js/modules/observers/article-section-headings-intersection-observer.js
adds the highlight to the menu links and the underline to thesection
headings.- The
js/modules/find-link.js
function is imported.
- The
js/modules/observers/primary-navigation-resize-observer.js
dynamically calculates the height of thenav
. The resulting value is printed as astyle
declaration on thehtml
element usingscroll-padding-top
.
- Additionally,
js/modules/theme-switcher.js
provides dark mode toggle functionality. The selected mode is saved to local storage.
grid
used for page layout.flexbox
used for page elements.- Files are organised using
@import
to pull modules intoindex.css
. - Files are organised internally using CSS nesting.
- Dark theme option.
Windows 10:
- Chrome
- Firefox
- Microsoft Edge
The page has been tested in both browser and device views.
- Fixed Header Anchor Navigation. An earlier version. I consider the current version using
position: sticky
instead ofposition: fixed
to be superior. The earlier version also lacks menu link and section header highlighting.