File tree Expand file tree Collapse file tree 1 file changed +16
-6
lines changed
Expand file tree Collapse file tree 1 file changed +16
-6
lines changed Original file line number Diff line number Diff line change 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 };
You can’t perform that action at this time.
0 commit comments