Dont use scrollIntoView as it causes the main content to scroll as well

This commit is contained in:
Kovid Goyal 2021-07-21 16:39:57 +05:30
parent 5027bc743b
commit 61f5d3972f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -1,5 +1,5 @@
/* vim:fileencoding=utf-8
*
*
* Copyright (C) 2021 Kovid Goyal <kovid at kovidgoyal.net>
*
* Distributed under terms of the GPLv3 license
@ -12,8 +12,20 @@ function get_sidebar_tree() {
return document.querySelector('.sidebar-tree');
}
function scroll_sidebar_node_into_view(a) {
var ss = get_sidebar_tree().closest('.sidebar-scroll');
if (!ss || !a) return;
ss.style.position = 'relative';
var pos = 0;
while (true) {
pos += a.offsetTop;
a = a.offsetParent;
if (!a || a == ss) break;
}
ss.scrollTop = pos;
}
function mark_current_link(sidebar_tree, a, onload) {
console.log(onload);
var li = a.closest('li.has-children');
while (li) {
li.querySelector('input[type=checkbox]').setAttribute('checked', 'checked');
@ -22,7 +34,7 @@ function mark_current_link(sidebar_tree, a, onload) {
sidebar_tree.querySelectorAll('.current').forEach(function (elem) {
elem.classList.remove('current');
});
if (onload) a.scrollIntoView();
if (onload) scroll_sidebar_node_into_view(a);
a.classList.add('current');
}
@ -32,7 +44,7 @@ function show_hash_in_sidebar(onload) {
var a = sidebar_tree.querySelector('a[href="' + document.location.hash + '"]');
if (a) mark_current_link(sidebar_tree, a, onload);
} else {
if (onload) sidebar_tree.querySelector('.current-page a').scrollIntoView();
if (onload) scroll_sidebar_node_into_view(sidebar_tree.querySelector('.current-page a'));
}
}