|
| 1 | +<style> |
| 2 | + body { |
| 3 | + display: flex; |
| 4 | + font-family: Arial, sans-serif; |
| 5 | + } |
| 6 | + #content { |
| 7 | + flex: 1; |
| 8 | + padding: 20px; |
| 9 | + max-width: 600px; |
| 10 | + margin-right: 20px; |
| 11 | + } |
| 12 | + #sidebar { |
| 13 | + width: 200px; |
| 14 | + position: fixed; |
| 15 | + top: 20px; |
| 16 | + right: 20px; |
| 17 | + } |
| 18 | + #sidebar ul { |
| 19 | + list-style-type: none; |
| 20 | + padding: 0; |
| 21 | + } |
| 22 | + #sidebar li { |
| 23 | + padding: 5px; |
| 24 | + cursor: pointer; |
| 25 | + } |
| 26 | + #sidebar li.active { |
| 27 | + font-weight: bold; |
| 28 | + color: blue; |
| 29 | + } |
| 30 | +</style> |
| 31 | + |
| 32 | +<div id="content"> |
| 33 | + <h1>Heading 1</h1> |
| 34 | + <p>Some content under heading 1.</p> |
| 35 | + <h2>Subheading 1.1</h2> |
| 36 | + <p>Some content under subheading 1.1.</p> |
| 37 | + <h3>Sub-subheading 1.1.1</h3> |
| 38 | + <p>Some content under sub-subheading 1.1.1.</p> |
| 39 | + <h2>Subheading 1.2</h2> |
| 40 | + <p>Some content under subheading 1.2.</p> |
| 41 | + <h1>Heading 2</h1> |
| 42 | + <p>Some content under heading 2.</p> |
| 43 | + <h2>Subheading 2.1</h2> |
| 44 | + <p>Some content under subheading 2.1.</p> |
| 45 | + <h3>Sub-subheading 2.1.1</h3> |
| 46 | + <p>Some content under sub-subheading 2.1.1.</p> |
| 47 | +</div> |
| 48 | + |
| 49 | +<div id="sidebar"> |
| 50 | + <h3>Table of Contents</h3> |
| 51 | + <ul id="toc"></ul> |
| 52 | +</div> |
| 53 | + |
| 54 | +<script> |
| 55 | + document.addEventListener("DOMContentLoaded", function() { |
| 56 | + const toc = document.getElementById("toc"); |
| 57 | + const headers = document.querySelectorAll("#content h1, #content h2, #content h3, #content h4, #content h5, #content h6"); |
| 58 | + |
| 59 | + headers.forEach(header => { |
| 60 | + const li = document.createElement("li"); |
| 61 | + li.textContent = header.textContent; |
| 62 | + li.dataset.target = header.id || header.textContent.replace(/\s+/g, '-').toLowerCase(); |
| 63 | + header.id = li.dataset.target; |
| 64 | + |
| 65 | + li.addEventListener("click", function() { |
| 66 | + document.getElementById(header.id).scrollIntoView({ behavior: "smooth" }); |
| 67 | + }); |
| 68 | + |
| 69 | + const level = parseInt(header.tagName.charAt(1), 10); |
| 70 | + if (level === 1) { |
| 71 | + toc.appendChild(li); |
| 72 | + } else { |
| 73 | + let parentLi = toc.querySelector(`li[data-target="${header.parentElement.id}"]`); |
| 74 | + if (parentLi) { |
| 75 | + let ul = parentLi.querySelector("ul"); |
| 76 | + if (!ul) { |
| 77 | + ul = document.createElement("ul"); |
| 78 | + parentLi.appendChild(ul); |
| 79 | + } |
| 80 | + ul.appendChild(li); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + const options = { |
| 86 | + root: null, |
| 87 | + threshold: 0.5, |
| 88 | + }; |
| 89 | + |
| 90 | + const callback = (entries) => { |
| 91 | + entries.forEach(entry => { |
| 92 | + const id = entry.target.id; |
| 93 | + const li = toc.querySelector(`li[data-target="${id}"]`); |
| 94 | + if (entry.isIntersecting) { |
| 95 | + if (li) { |
| 96 | + li.classList.add("active"); |
| 97 | + } |
| 98 | + } else { |
| 99 | + if (li) { |
| 100 | + li.classList.remove("active"); |
| 101 | + } |
| 102 | + } |
| 103 | + }); |
| 104 | + }; |
| 105 | + |
| 106 | + const observer = new IntersectionObserver(callback, options); |
| 107 | + headers.forEach(header => observer.observe(header)); |
| 108 | + }); |
| 109 | +</script> |
0 commit comments