-
- function addNavObserver(headings) {
- // Setup the intersection observer.
- let intersectOpts = {
- rootMargin: '0px 0px 0px 0px',
- threshold: 1.0
- };
- let pageNavObserver = new IntersectionObserver(headingVisibilityChange, intersectOpts);
-
- // observe each heading
- for (let heading of headings) {
- pageNavObserver.observe(heading);
- }
- }
-
- function headingVisibilityChange(entries, observer) {
- for (let entry of entries) {
- let isVisible = (entry.intersectionRatio === 1);
- toggleAnchorHighlighting(entry.target.id, isVisible);
- }
- }
-
- function toggleAnchorHighlighting(elementId, shouldHighlight) {
- DOM.forEach('a[href="#' + elementId + '"]', anchor => {
- anchor.closest('li').classList.toggle('current-heading', shouldHighlight);
- });
- }