Skip to content

Commit

Permalink
fix: update and rerun scripts in post details page after swap
Browse files Browse the repository at this point in the history
  • Loading branch information
satnaing committed Sep 16, 2024
1 parent 6b05423 commit dd07c2e
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/layouts/PostDetails.astro
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const nextPost =
}
</style>

<script is:inline>
<script is:inline data-astro-rerun>
/** Create a progress indicator
* at the top */
function createProgressBar() {
Expand All @@ -212,20 +212,22 @@ const nextPost =
/** Update the progress bar
* when user scrolls */
function updateScrollProgress() {
const winScroll =
document.body.scrollTop || document.documentElement.scrollTop;
const height =
document.documentElement.scrollHeight -
document.documentElement.clientHeight;
const scrolled = (winScroll / height) * 100;
if (document) {
const myBar = document.getElementById("myBar");
if (myBar) {
myBar.style.width = scrolled + "%";
document.addEventListener("scroll", () => {
const winScroll =
document.body.scrollTop || document.documentElement.scrollTop;
const height =
document.documentElement.scrollHeight -
document.documentElement.clientHeight;
const scrolled = (winScroll / height) * 100;
if (document) {
const myBar = document.getElementById("myBar");
if (myBar) {
myBar.style.width = scrolled + "%";
}
}
}
});
}
document.addEventListener("scroll", updateScrollProgress);
updateScrollProgress();

/** Attaches links to headings in the document,
* allowing sharing of sections easily */
Expand Down Expand Up @@ -300,5 +302,9 @@ const nextPost =
});
}
backToTop();
document.addEventListener("astro:after-swap", backToTop);

/* Go to page start after page swap */
document.addEventListener("astro:after-swap", () =>
window.scrollTo({ left: 0, top: 0, behavior: "instant" })
);
</script>

0 comments on commit dd07c2e

Please sign in to comment.