Skip to content

Commit ae5082b

Browse files
authored
back
1 parent 213bd65 commit ae5082b

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

docs/_layouts/on-this-page.html

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,38 @@
3636
{{ content }}
3737
</div>
3838
</div>
39-
4039
<script>
4140
document.addEventListener("DOMContentLoaded", function() {
42-
if (window.innerWidth <= 800) {
43-
const toc = document.querySelector(".toc");
44-
const firstHeader = document.querySelector("h1");
45-
if (toc && firstHeader) {
46-
firstHeader.insertAdjacentElement("afterend", toc);
41+
const toc = document.querySelector(".toc");
42+
const firstHeader = document.querySelector("h1");
43+
const originalParent = toc?.parentNode;
44+
const originalNextSibling = toc?.nextSibling;
45+
46+
function moveTOC() {
47+
if (!toc || !firstHeader) return;
48+
49+
if (window.innerWidth <= 800) {
50+
// Move TOC after first header
51+
if (firstHeader.nextElementSibling !== toc) {
52+
firstHeader.insertAdjacentElement("afterend", toc);
53+
}
54+
} else {
55+
// Put TOC back in original place
56+
if (originalParent && toc.parentNode !== originalParent) {
57+
if (originalNextSibling) {
58+
originalParent.insertBefore(toc, originalNextSibling);
59+
} else {
60+
originalParent.appendChild(toc);
61+
}
62+
}
4763
}
4864
}
65+
66+
// Run on load
67+
moveTOC();
68+
69+
// Run on resize
70+
window.addEventListener("resize", moveTOC);
4971
});
5072
</script>
5173

0 commit comments

Comments
 (0)