|
| 1 | +/** |
| 2 | + * Floating Table of Contents with Scroll Spy |
| 3 | + * Highlights the current section as user scrolls through the page |
| 4 | + */ |
| 5 | +(function () { |
| 6 | + "use strict"; |
| 7 | + |
| 8 | + // Wait for DOM to be ready |
| 9 | + if (document.readyState === "loading") { |
| 10 | + document.addEventListener("DOMContentLoaded", initTOC); |
| 11 | + } else { |
| 12 | + initTOC(); |
| 13 | + } |
| 14 | + |
| 15 | + function initTOC() { |
| 16 | + const toc = document.querySelector(".floating-toc"); |
| 17 | + if (!toc) return; // Exit if no TOC on page |
| 18 | + |
| 19 | + const tocLinks = toc.querySelectorAll("a"); |
| 20 | + if (tocLinks.length === 0) return; |
| 21 | + |
| 22 | + // Handle TOC visibility based on scroll position |
| 23 | + function updateTOCVisibility() { |
| 24 | + const scrollPosition = |
| 25 | + window.pageYOffset || document.documentElement.scrollTop; |
| 26 | + |
| 27 | + // Find the article content area to determine when to show TOC |
| 28 | + const articleContent = document.querySelector(".singleContent"); |
| 29 | + |
| 30 | + if (articleContent) { |
| 31 | + // Show TOC once user scrolls past the article start |
| 32 | + const articleTop = |
| 33 | + articleContent.getBoundingClientRect().top + window.pageYOffset; |
| 34 | + const triggerPoint = articleTop - 200; // Show 200px before content |
| 35 | + |
| 36 | + if (scrollPosition > triggerPoint) { |
| 37 | + toc.classList.add("visible"); |
| 38 | + } else { |
| 39 | + toc.classList.remove("visible"); |
| 40 | + } |
| 41 | + } else { |
| 42 | + // Fallback: show after scrolling 600px (roughly past banner) |
| 43 | + if (scrollPosition > 600) { |
| 44 | + toc.classList.add("visible"); |
| 45 | + } else { |
| 46 | + toc.classList.remove("visible"); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Initialize visibility on page load |
| 52 | + updateTOCVisibility(); |
| 53 | + |
| 54 | + // Update visibility on scroll (throttled for performance) |
| 55 | + let visibilityTimeout; |
| 56 | + window.addEventListener( |
| 57 | + "scroll", |
| 58 | + function () { |
| 59 | + if (!visibilityTimeout) { |
| 60 | + visibilityTimeout = setTimeout(function () { |
| 61 | + updateTOCVisibility(); |
| 62 | + visibilityTimeout = null; |
| 63 | + }, 50); |
| 64 | + } |
| 65 | + }, |
| 66 | + { passive: true }, |
| 67 | + ); |
| 68 | + |
| 69 | + // Get all H2 heading elements that have IDs (these are TOC targets) |
| 70 | + const headings = Array.from(document.querySelectorAll("h2[id]")).filter( |
| 71 | + (heading) => { |
| 72 | + // Only include headings in the main content area |
| 73 | + const singleContent = document.querySelector(".singleContent"); |
| 74 | + return singleContent && singleContent.contains(heading); |
| 75 | + }, |
| 76 | + ); |
| 77 | + |
| 78 | + if (headings.length === 0) return; |
| 79 | + |
| 80 | + // Smooth scroll when clicking TOC links |
| 81 | + tocLinks.forEach((link) => { |
| 82 | + link.addEventListener("click", function (e) { |
| 83 | + const href = this.getAttribute("href"); |
| 84 | + if (href && href.startsWith("#")) { |
| 85 | + e.preventDefault(); |
| 86 | + const targetId = href.substring(1); |
| 87 | + const targetElement = document.getElementById(targetId); |
| 88 | + |
| 89 | + if (targetElement) { |
| 90 | + const headerHeight = 100; // Adjust based on your fixed header height |
| 91 | + const targetPosition = |
| 92 | + targetElement.getBoundingClientRect().top + |
| 93 | + window.pageYOffset - |
| 94 | + headerHeight; |
| 95 | + |
| 96 | + window.scrollTo({ |
| 97 | + top: targetPosition, |
| 98 | + behavior: "smooth", |
| 99 | + }); |
| 100 | + } |
| 101 | + } |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + // Intersection Observer for scroll spy |
| 106 | + const observerOptions = { |
| 107 | + rootMargin: "-100px 0px -66%", |
| 108 | + threshold: 0, |
| 109 | + }; |
| 110 | + |
| 111 | + let activeHeading = null; |
| 112 | + |
| 113 | + const observer = new IntersectionObserver((entries) => { |
| 114 | + entries.forEach((entry) => { |
| 115 | + if (entry.isIntersecting) { |
| 116 | + const id = entry.target.id; |
| 117 | + activeHeading = id; |
| 118 | + updateActiveTOCLink(id); |
| 119 | + } |
| 120 | + }); |
| 121 | + }, observerOptions); |
| 122 | + |
| 123 | + // Observe all headings |
| 124 | + headings.forEach((heading) => observer.observe(heading)); |
| 125 | + |
| 126 | + // Update active link styling |
| 127 | + function updateActiveTOCLink(activeId) { |
| 128 | + // Remove active class from all links |
| 129 | + tocLinks.forEach((link) => link.classList.remove("active")); |
| 130 | + |
| 131 | + // Add active class to current link |
| 132 | + if (activeId) { |
| 133 | + const activeLink = toc.querySelector(`a[href="#${activeId}"]`); |
| 134 | + if (activeLink) { |
| 135 | + activeLink.classList.add("active"); |
| 136 | + |
| 137 | + // Scroll the TOC if needed to keep active link visible |
| 138 | + const tocNav = toc.querySelector(".toc-nav"); |
| 139 | + if (tocNav) { |
| 140 | + const linkRect = activeLink.getBoundingClientRect(); |
| 141 | + const navRect = tocNav.getBoundingClientRect(); |
| 142 | + |
| 143 | + if ( |
| 144 | + linkRect.bottom > navRect.bottom || |
| 145 | + linkRect.top < navRect.top |
| 146 | + ) { |
| 147 | + activeLink.scrollIntoView({ |
| 148 | + block: "nearest", |
| 149 | + behavior: "smooth", |
| 150 | + }); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // Handle initial scroll position on page load |
| 158 | + function setInitialActiveLink() { |
| 159 | + const scrollPosition = window.pageYOffset; |
| 160 | + let currentHeading = null; |
| 161 | + |
| 162 | + for (let i = headings.length - 1; i >= 0; i--) { |
| 163 | + const heading = headings[i]; |
| 164 | + if (heading.offsetTop <= scrollPosition + 150) { |
| 165 | + currentHeading = heading.id; |
| 166 | + break; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if (currentHeading) { |
| 171 | + updateActiveTOCLink(currentHeading); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // Set initial active state |
| 176 | + setInitialActiveLink(); |
| 177 | + |
| 178 | + // Fallback: throttled scroll event listener for browsers with poor Intersection Observer support |
| 179 | + let activeLinkTimeout; |
| 180 | + window.addEventListener( |
| 181 | + "scroll", |
| 182 | + function () { |
| 183 | + if (activeLinkTimeout) { |
| 184 | + window.cancelAnimationFrame(activeLinkTimeout); |
| 185 | + } |
| 186 | + |
| 187 | + activeLinkTimeout = window.requestAnimationFrame(function () { |
| 188 | + setInitialActiveLink(); |
| 189 | + }); |
| 190 | + }, |
| 191 | + { passive: true }, |
| 192 | + ); |
| 193 | + } |
| 194 | +})(); |
0 commit comments