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
navand 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
sectionheading, (on click or on scroll) . - It dynamically calculates the height of the
navas 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-observeradds / removes thenavshadow and the 'Back to top' link on scroll.js/modules/observers/article-section-headings-intersection-observer.jsadds the highlight to the menu links and the underline to thesectionheadings.- The
js/modules/find-link.jsfunction is imported.
- The
js/modules/observers/primary-navigation-resize-observer.jsdynamically calculates the height of thenav. The resulting value is printed as astyledeclaration on thehtmlelement usingscroll-padding-top.
- Additionally,
js/modules/theme-switcher.jsprovides dark mode toggle functionality. The selected mode is saved to local storage.
gridused for page layout.flexboxused for page elements.- Files are organised using
@importto 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: stickyinstead ofposition: fixedto be superior. The earlier version also lacks menu link and section header highlighting.