js: Refactor go-to-top handler

href # doesn't work... probably because the wrapper
This commit is contained in:
Franciszek Stachura 2024-09-23 14:09:54 +02:00
parent 61ca8e19f2
commit 2f7b56a4d8

View file

@ -128,14 +128,6 @@ function setupSidebarSwitch() {
});
}
var wrapper = document.querySelector('.wrapper')
/* Linenumbers navigation */
document.querySelector('.go-top').onclick = function() {
wrapper.scrollTop = 0
wrapper.scrollLeft = 0
}
// When using linenumbers's anchor
// it jump the line a the top of the page
// and it's hidden under the fixed topbar element.
@ -285,6 +277,16 @@ function setupLineRangeHandlers() {
});
}
function setupGoToTop() {
const wrapper = document.querySelector('.wrapper');
const goToTop = document.querySelector('.go-top');
goToTop.addEventListener('click', e => {
wrapper.scrollTop = 0;
wrapper.scrollLeft = 0;
});
}
// recalculate scroll when page is fully loaded
// in case of slow rendering very long pages.
window.onload = function () {
@ -297,6 +299,8 @@ window.onload = function () {
handleLineRange(window.location.hash);
setupLineRangeHandlers();
setupGoToTop();
// fix incorrectly issued 301 redirect
// https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
// https://developer.mozilla.org/en-US/docs/Web/API/Request/redirect