Skip to content

Commit 8196b57

Browse files
authored
Merge pull request #45 from rockoder/fix-reading-progress-start-9015881955667487806
Fix reading progress bar initial value
2 parents 1fcf6ee + e57eed2 commit 8196b57

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/components/ReadingProgress.astro

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,25 @@
1010

1111
if (progressBar && article) {
1212
const updateProgress = () => {
13-
const articleTop = article.offsetTop;
14-
const articleHeight = article.offsetHeight;
13+
const rect = article.getBoundingClientRect();
14+
const articleTop = rect.top + window.scrollY;
15+
const articleHeight = rect.height;
1516
const windowHeight = window.innerHeight;
1617
const scrollY = window.scrollY;
1718

18-
// Calculate progress within the article
19-
const scrollableDistance = articleHeight - windowHeight + articleTop;
20-
const scrolled = Math.max(0, scrollY - articleTop + windowHeight);
21-
const progress = Math.min(100, (scrolled / scrollableDistance) * 100);
19+
// Calculate the total scrollable distance from the top of the page
20+
// to the point where the bottom of the article reaches the bottom of the viewport.
21+
const scrollRange = articleTop + articleHeight - windowHeight;
22+
23+
let progress = 0;
24+
if (scrollRange > 0) {
25+
// Start at 0% when the page is at the top (scrollY = 0)
26+
// and reach 100% at the end of the article.
27+
progress = Math.max(0, Math.min(100, (scrollY / scrollRange) * 100));
28+
} else {
29+
// If the article is already fully visible, progress is 100%.
30+
progress = 100;
31+
}
2232

2333
progressBar.style.width = `${progress}%`;
2434
};

0 commit comments

Comments
 (0)