Skip to content

Commit 140258e

Browse files
committed
feat: openDetailsForHash with scrollIntoView
1 parent e654f06 commit 140258e

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/assets/js/app.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
document.addEventListener("DOMContentLoaded", () => {
22
anchors.options = { visible: "always" };
33
anchors.add();
4+
5+
window.addEventListener("hashchange", openDetailsForHash);
6+
openDetailsForHash();
47
});
58

69
$(() => {
@@ -10,19 +13,6 @@ $(() => {
1013
});
1114
hljs.highlightAll();
1215

13-
// Open details that is at the same level as the target of the hash
14-
window.addEventListener("hashchange", () => {
15-
const target = document.querySelector(window.location.hash);
16-
if (!target) return;
17-
18-
const siblingDetails = target
19-
.closest("div.method")
20-
?.querySelector("details");
21-
if (siblingDetails) {
22-
siblingDetails.open = true;
23-
}
24-
});
25-
2616
$("#navigation").load(`${config.rootPath}navigation.html`, () => {
2717
$(".sidebar-sticky .icon").on("click", function (e) {
2818
e.preventDefault();
@@ -35,3 +25,17 @@ $(() => {
3525
.show();
3626
});
3727
});
28+
29+
// Open details that is at the same level as the target of the hash
30+
function openDetailsForHash() {
31+
const hash = window.location.hash;
32+
if (!hash) return;
33+
const target = document.querySelector(hash);
34+
if (!target) return;
35+
36+
const siblingDetails = target.closest("div.method")?.querySelector("details");
37+
if (siblingDetails) {
38+
siblingDetails.open = true;
39+
target.scrollIntoView({ behavior: "smooth", block: "start" });
40+
}
41+
}

0 commit comments

Comments
 (0)