|
22 | 22 |
|
23 | 23 | const debugMode = false;
|
24 | 24 |
|
25 |
| - const tocElement = document.querySelector("#toc"); |
26 |
| - const toggleTocElement = document.querySelector("#toggle-toc"); |
27 |
| - const contentElement = document.querySelector("#content"); |
| 25 | + let tocElement; |
| 26 | + let toggleTocElement; |
| 27 | + let contentElement; |
28 | 28 |
|
29 |
| - if (!tocElement || !contentElement) { |
30 |
| - return; |
31 |
| - } |
32 |
| - |
33 |
| - const headingElements = findHeadingElements(); |
34 |
| - const hrefToTocAnchorElement = buildHrefToTocAnchorElement(); |
35 |
| - const headingElementToTocElement = buildHeadingElementToTocElement(); |
| 29 | + let headingElements; |
| 30 | + let hrefToTocAnchorElement; |
| 31 | + let headingElementToTocElement; |
36 | 32 |
|
37 | 33 | let lastActiveTocElement = null;
|
38 | 34 | let disableOnScroll = false;
|
39 | 35 |
|
40 |
| - tocElement.addEventListener("click", onTocElementClick); |
41 |
| - toggleTocElement.addEventListener("click", onToggleTocClick); |
42 | 36 | window.addEventListener("load", onLoad);
|
43 | 37 |
|
| 38 | + function onLoad() { |
| 39 | + tocElement = document.querySelector("#toc"); |
| 40 | + toggleTocElement = document.querySelector("#toggle-toc"); |
| 41 | + contentElement = document.querySelector("#content"); |
| 42 | + if (!tocElement || !contentElement) { |
| 43 | + return; |
| 44 | + } |
| 45 | + headingElements = findHeadingElements(); |
| 46 | + hrefToTocAnchorElement = buildHrefToTocAnchorElement(); |
| 47 | + headingElementToTocElement = buildHeadingElementToTocElement(); |
| 48 | + onLocationHashChange(); |
| 49 | + window.addEventListener("hashchange", onLocationHashChange); |
| 50 | + window.addEventListener("scroll", onScroll); |
| 51 | + window.addEventListener("scroll", onEndScroll); |
| 52 | + window.addEventListener("resize", onResize); |
| 53 | + tocElement.addEventListener("click", onTocElementClick); |
| 54 | + toggleTocElement.addEventListener("click", onToggleTocClick); |
| 55 | + } |
| 56 | + |
44 | 57 | function findHeadingElements() {
|
45 | 58 | const maxHeadingLevel = getMaxHeadingLevel();
|
46 | 59 | const headingSelectors = [];
|
|
101 | 114 | return result;
|
102 | 115 | }
|
103 | 116 |
|
104 |
| - function onLoad() { |
105 |
| - onLocationHashChange(); |
106 |
| - window.addEventListener("hashchange", onLocationHashChange); |
107 |
| - window.addEventListener("scroll", onScroll); |
108 |
| - window.addEventListener("scroll", onEndScroll); |
109 |
| - window.addEventListener("resize", onResize); |
110 |
| - } |
| 117 | + |
111 | 118 |
|
112 | 119 | function onLocationHashChange() {
|
113 | 120 | updateFixedPositionClass();
|
|
0 commit comments