Add better scroll position management
This commit is contained in:
parent
960f7ff4d0
commit
cc21c06641
@ -1,7 +1,7 @@
|
|||||||
// Client-side script that runs on page load
|
// Client-side script that runs on page load
|
||||||
// Example: TypeScript with type annotations
|
// Example: TypeScript with type annotations
|
||||||
|
|
||||||
async function loadContent(url: string) {
|
async function loadContent(url: string, shouldScrollToTop: boolean = true) {
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
headers: {
|
headers: {
|
||||||
'shell-loaded': 'true'
|
'shell-loaded': 'true'
|
||||||
@ -13,6 +13,10 @@ async function loadContent(url: string) {
|
|||||||
mainElement.outerHTML = html;
|
mainElement.outerHTML = html;
|
||||||
// Re-attach handlers to new links after content swap
|
// Re-attach handlers to new links after content swap
|
||||||
attachLinkHandlers();
|
attachLinkHandlers();
|
||||||
|
// Smooth scroll to top after navigation if needed
|
||||||
|
if (shouldScrollToTop) {
|
||||||
|
window.scrollTo({ top: 0, behavior: 'smooth' });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,14 +31,14 @@ function attachLinkHandlers() {
|
|||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
e.stopImmediatePropagation();
|
e.stopImmediatePropagation();
|
||||||
window.history.pushState({}, '', link.href);
|
window.history.pushState({}, '', link.href);
|
||||||
await loadContent(link.href);
|
await loadContent(link.href, true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen for back/forward button clicks
|
// Listen for back/forward button clicks
|
||||||
window.addEventListener('popstate', async (event) => {
|
window.addEventListener('popstate', async (event) => {
|
||||||
await loadContent(window.location.href);
|
await loadContent(window.location.href, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user