Skip to content

Commit 1403dab

Browse files
committed
fix: modify the scroll implementation to prevent it from affecting the window scroll
1 parent 3cc7c73 commit 1403dab

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

js/common.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Mousetrap.bind("b o r k", function () {
104104
function cycle(to, from) {
105105
from.removeClass("current");
106106
to.addClass("current");
107-
to[0].scrollIntoView({ block: "center" });
107+
scrollToActiveItem();
108108
}
109109

110110
function getNextOrPreviousSibling(node, forward) {
@@ -142,6 +142,18 @@ function cycleHeaders(matches, forward) {
142142
cycle($(matches[forward ? 0 : matches.length - 1]), $(matches[forward ? matches.length - 1 : 0]));
143143
}
144144
}
145+
146+
function scrollToActiveItem() {
147+
const sidebar = document.querySelector(".layout-menu");
148+
const activeItem = sidebar.querySelector(".current");
149+
150+
if (sidebar && activeItem) {
151+
sidebar.scrollTo({
152+
top: activeItem.offsetTop - sidebar.offsetTop - (sidebar.clientHeight / 2) + (activeItem.clientHeight / 2)
153+
});
154+
}
155+
}
156+
145157
Mousetrap.bind("j", function () {
146158
/* Doc page */
147159
var node = $(".layout-menu .current");
@@ -689,10 +701,7 @@ $(document).ready(function () {
689701
});
690702
/* }}} */
691703

692-
const activeItem = document.querySelector(".layout-menu .current");
693-
if (activeItem) {
694-
activeItem.scrollIntoView({ block: "center" });
695-
}
704+
scrollToActiveItem();
696705
});
697706

698707
/* {{{ add-user.php animations */

0 commit comments

Comments
 (0)