Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add scroll indicator in blog posts #249

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading