Skip to content

Commit 9c2cb5b

Browse files
added scrolling progress bar below header to blog posts
1 parent 182ba06 commit 9c2cb5b

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

src/layouts/Layout.astro

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,19 @@ const { title, description = "Just a dude who enjoys writing code & learning;" }
7171
button.style.right = `${nextRight}px`;
7272
};
7373

74+
const progressBar = document.querySelector('[data-reading-progress]');
75+
7476
const updateVisibility = () => {
7577
const shouldShow = window.scrollY > threshold;
7678
button.classList.toggle('opacity-0', !shouldShow);
7779
button.classList.toggle('translate-y-2', !shouldShow);
7880
button.classList.toggle('pointer-events-none', !shouldShow);
81+
82+
if (progressBar) {
83+
const scrollable = document.documentElement.scrollHeight - window.innerHeight;
84+
const progress = scrollable > 0 ? Math.min(window.scrollY / scrollable, 1) : 1;
85+
progressBar.style.transform = `scaleX(${progress})`;
86+
}
7987
};
8088

8189
button.addEventListener('click', () => {

src/pages/blog/[...slug].astro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ const readingTime = getReadingTimeLabel(post.body);
2121

2222
<PostHogLayout>
2323
<Layout title={post.data.title} description={post.data.description}>
24+
<div
25+
data-reading-progress
26+
style="transform: scaleX(0);"
27+
class="fixed top-16 left-0 w-full z-50 h-[3px] bg-blog-accent origin-left"
28+
></div>
2429
<section class="py-10 max-w-3xl mx-auto">
2530
<article>
2631
<div class="mb-10 text-center">

0 commit comments

Comments
 (0)