Skip to content

Commit

Permalink
feat: add scroll indicator in blog posts (satnaing#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
satnaing authored Feb 9, 2024
1 parent 556df2d commit 8e022c7
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/layouts/PostDetails.astro
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ const layoutProps = {

<Layout {...layoutProps}>
<Header />

<!-- <div class="progress-container fixed top-0 z-10 h-1 w-full bg-skin-fill">
<div class="progress-bar h-1 w-0 bg-skin-accent" id="myBar"></div>
</div> -->

<div class="mx-auto flex w-full max-w-3xl justify-start px-2">
<button
class="focus-outline mb-2 mt-8 flex hover:opacity-75"
Expand Down Expand Up @@ -107,6 +112,45 @@ const layoutProps = {
</style>

<script is:inline>
/** Create a progress indicator
* at the top */
function createProgressBar() {
// Create the main container div
const progressContainer = document.createElement("div");
progressContainer.className =
"progress-container fixed top-0 z-10 h-1 w-full bg-skin-fill";

// Create the progress bar div
const progressBar = document.createElement("div");
progressBar.className = "progress-bar h-1 w-0 bg-skin-accent";
progressBar.id = "myBar";

// Append the progress bar to the progress container
progressContainer.appendChild(progressBar);

// Append the progress container to the document body or any other desired parent element
document.body.appendChild(progressContainer);
}
createProgressBar();

/** 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", updateScrollProgress);

/** Attaches links to headings in the document,
* allowing sharing of sections easily */
function addHeadingLinks() {
Expand Down

0 comments on commit 8e022c7

Please sign in to comment.