Skip to content

Commit 8eeddfb

Browse files
committed
feat: scroll api docs sidebar automatically into current viewport
1 parent ae98bbf commit 8eeddfb

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

apify-docs-theme/static/js/custom.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,43 @@
3232
// load();
3333
// }
3434
// }, 500);
35+
36+
let lastKnownScrollHash = '';
37+
let ticking = false;
38+
39+
function doSomething() {
40+
const hash = window.location.hash.substring(1);
41+
42+
if (hash !== lastKnownScrollHash) {
43+
console.log(hash, `li[data-item-id="${hash}"]`);
44+
const $li = document.querySelector(`li[data-item-id="${hash}"]`);
45+
46+
if (!$li) {
47+
return;
48+
}
49+
50+
// not visible, click on the parent <li> first
51+
if (!$li.offsetParent) {
52+
$li.parentElement?.parentElement?.click();
53+
}
54+
55+
$li.scrollIntoView({
56+
// smooth would be nice, but it's not working in some case
57+
// behavior: 'smooth',
58+
block: 'nearest',
59+
inline: 'center',
60+
});
61+
lastKnownScrollHash = hash;
62+
}
63+
}
64+
65+
document.addEventListener('scroll', () => {
66+
if (!ticking) {
67+
window.requestAnimationFrame(() => {
68+
doSomething();
69+
ticking = false;
70+
});
71+
72+
ticking = true;
73+
}
74+
});

0 commit comments

Comments
 (0)