Skip to content

Commit b3bd7d8

Browse files
Solved the broken progress bar issue
1 parent 56e17a3 commit b3bd7d8

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

src/components/ScrollProgressBar.tsx

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ const ScrollProgressBar = () => {
44
const [scrollWidth, setScrollWidth] = useState(0);
55
const [isAnimating, setIsAnimating] = useState(true); // Tracks if the page load animation is active
66

7+
const handleScroll = () => {
8+
const scrollTop = document.documentElement.scrollTop;
9+
const scrollHeight =
10+
document.documentElement.scrollHeight -
11+
document.documentElement.clientHeight;
12+
const width = (scrollTop / scrollHeight) * 100;
13+
setScrollWidth(width);
14+
};
15+
716
useEffect(() => {
817
// Simulate the page load animation
918
const animationTimeout = setTimeout(() => {
@@ -14,21 +23,13 @@ const ScrollProgressBar = () => {
1423
return () => clearTimeout(animationTimeout);
1524
}, []);
1625

17-
const handleScroll = () => {
18-
const scrollTop = document.documentElement.scrollTop;
19-
const scrollHeight =
20-
document.documentElement.scrollHeight -
21-
document.documentElement.clientHeight;
22-
const width = (scrollTop / scrollHeight) * 100;
23-
setScrollWidth(width);
24-
};
25-
2626
useEffect(() => {
27-
if (!isAnimating) {
28-
window.addEventListener("scroll", handleScroll);
29-
}
27+
// Always listen for scroll events
28+
window.addEventListener("scroll", handleScroll);
29+
// Call handleScroll once on mount to set initial position
30+
handleScroll();
3031
return () => window.removeEventListener("scroll", handleScroll);
31-
}, [isAnimating]);
32+
}, []);
3233

3334
return (
3435
<>
@@ -56,10 +57,10 @@ const ScrollProgressBar = () => {
5657
top: 0,
5758
left: 0,
5859
width: `${scrollWidth}%`,
59-
height: "5px", // Thicker line for scroll progress
60-
backgroundColor: "grey",
60+
height: "3px",
61+
backgroundColor: "#3b82f6",
6162
zIndex: 100,
62-
transition: "width 0.2s ease",
63+
transition: "width 0.1s ease",
6364
}}
6465
/>
6566
)}

0 commit comments

Comments
 (0)