Skip to content

Commit 5938ea2

Browse files
committed
Fix links with anchors nested in details not scrolling to the content
1 parent 216c0b5 commit 5938ea2

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

theme/footer.html

+50
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,55 @@
3535
}
3636
}
3737

38+
function scrollToAnchorNestedInDetailsIfNeeded() {
39+
function scrollToElement() {
40+
const id = window.location.hash.substr(1);
41+
if (id === '') {
42+
return;
43+
}
44+
45+
const element = document.getElementById(id);
46+
if (!element) {
47+
return;
48+
}
49+
50+
const details = document.getElementsByTagName('details');
51+
for (const element of details) {
52+
element.open = false;
53+
}
54+
55+
let temp = element;
56+
while (temp && temp.tagName) {
57+
if (temp.tagName.toLowerCase() === 'details') {
58+
temp.open = true;
59+
}
60+
61+
temp = temp.parentNode;
62+
}
63+
64+
element.scrollIntoView();
65+
}
66+
67+
function onReady(callback) {
68+
if (document.readyState !== 'loading') {
69+
callback();
70+
} else if (document.addEventListener) {
71+
document.addEventListener('DOMContentLoaded', callback);
72+
} else {
73+
document.attachEvent('onreadystatechange', function() {
74+
if (document.readyState === 'complete') {
75+
callback();
76+
}
77+
});
78+
}
79+
}
80+
81+
onReady(function () {
82+
scrollToElement();
83+
window.addEventListener('hashchange', scrollToElement);
84+
});
85+
}
86+
3887
ensureExternalLinksOpenInNewTab();
88+
scrollToAnchorNestedInDetailsIfNeeded();
3989
</script>

0 commit comments

Comments
 (0)