|
| 1 | +<aside id="toc-container" class="right"> |
| 2 | + <button id="toggle-toc">⇄</button> |
| 3 | + <!--<button id="toggle-toc-visibility">👁️</button>--> |
| 4 | + <h3>Index</h3> |
| 5 | + <nav id="toc"></nav> |
| 6 | +</aside> |
| 7 | + |
| 8 | +<script> |
| 9 | +document.addEventListener("DOMContentLoaded", function () { |
| 10 | + let headers = document.querySelectorAll("h1, h2, h3"); |
| 11 | + let toc = document.getElementById("toc"); |
| 12 | + |
| 13 | + headers.forEach((header, index) => { |
| 14 | + if (header.textContent.trim() === "Index") { |
| 15 | + return; // Si el encabezado es "Index", ignorarlo y continuar con el siguiente |
| 16 | + } |
| 17 | + |
| 18 | + if (!header.id) header.id = "header-" + index; // Asegura un ID único |
| 19 | + let link = document.createElement("a"); |
| 20 | + link.href = "#" + header.id; |
| 21 | + link.textContent = header.textContent; |
| 22 | + link.classList.add("toc-link", "level-" + header.tagName.toLowerCase()); |
| 23 | + toc.appendChild(link); |
| 24 | + }); |
| 25 | + |
| 26 | + // Resaltar sección activa al hacer scroll |
| 27 | + window.addEventListener("scroll", () => { |
| 28 | + let fromTop = window.scrollY + 20; |
| 29 | + let links = document.querySelectorAll(".toc-link"); |
| 30 | + |
| 31 | + links.forEach(link => { |
| 32 | + let section = document.querySelector(link.getAttribute("href")); |
| 33 | + if (section && section.offsetTop <= fromTop && section.offsetTop + section.offsetHeight > fromTop) { |
| 34 | + link.classList.add("active"); |
| 35 | + } else { |
| 36 | + link.classList.remove("active"); |
| 37 | + } |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + // Alternar posición (izquierda/derecha) |
| 42 | + document.getElementById("toggle-toc").addEventListener("click", function () { |
| 43 | + let tocContainer = document.getElementById("toc-container"); |
| 44 | + tocContainer.classList.toggle("left"); |
| 45 | + tocContainer.classList.toggle("right"); |
| 46 | + }); |
| 47 | + |
| 48 | + // Alternar visibilidad del índice |
| 49 | + document.getElementById("toggle-toc-visibility").addEventListener("click", function () { |
| 50 | + let tocContainer = document.getElementById("toc-container"); |
| 51 | + tocContainer.classList.toggle("hidden"); // Alterna la visibilidad |
| 52 | + }); |
| 53 | +}); |
| 54 | +</script> |
| 55 | + |
0 commit comments