Skip to content

Commit

Permalink
Scroll back to top on page change
Browse files Browse the repository at this point in the history
  • Loading branch information
Kale-Ko committed Jul 18, 2024
1 parent fb9135f commit dbfd8c7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 5 additions & 2 deletions scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async function fetchJsonCached(url, options) {
}
let originalTitle = document.title;
let currentUrl = new URL(window.location.href);
async function loadPage() {
async function loadPage(scrollToTop = false) {
console.group("Loading page...");
let parameters = new URLSearchParams(window.location.search);
let language = parameters.get("language");
Expand Down Expand Up @@ -163,7 +163,7 @@ async function fetchJsonCached(url, options) {
}
currentUrl = url;
history.pushState({ mode: "changePage" }, "", url);
loadPage();
loadPage(true);
});
}
else if (href.startsWith("#")) {
Expand Down Expand Up @@ -244,6 +244,9 @@ async function fetchJsonCached(url, options) {
await displayMarkdown(body.querySelector("#table-of-contents"), data);
}
async function markDone() {
if (scrollToTop) {
document.body.scrollTop = document.documentElement.scrollTop = 0;
}
body.classList.remove("loading");
body.classList.add("loaded");
if (window.location.hash.length > 0) {
Expand Down
8 changes: 6 additions & 2 deletions scripts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ interface VersionInfo {
let originalTitle: string = document.title;
let currentUrl: URL = new URL(window.location.href);

async function loadPage() {
async function loadPage(scrollToTop: boolean = false) {
console.group("Loading page...");

let parameters: URLSearchParams = new URLSearchParams(window.location.search);
Expand Down Expand Up @@ -217,7 +217,7 @@ interface VersionInfo {
currentUrl = url;
history.pushState({ mode: "changePage" }, "", url);

loadPage();
loadPage(true);
});
} else if (href.startsWith("#")) {
element.addEventListener("click", (event: MouseEvent): void => {
Expand Down Expand Up @@ -325,6 +325,10 @@ interface VersionInfo {
}

async function markDone(): Promise<void> {
if (scrollToTop) {
document.body.scrollTop = document.documentElement.scrollTop = 0;
}

body.classList.remove("loading");
body.classList.add("loaded");

Expand Down

0 comments on commit dbfd8c7

Please sign in to comment.