Skip to content

Commit

Permalink
fix(utils): handle edge cases in scroll percentage calculation #432
Browse files Browse the repository at this point in the history
- Add checks for NaN, negative, and infinite values
- Ensure percentage is capped at 100%
- Improve reliability of scroll progress indicator

Co-Authored-By: xzadudu179 <129601567+xzadudu179@users.noreply.github.com>
  • Loading branch information
EvanNotFound and xzadudu179 committed Sep 21, 2024
1 parent e58746e commit c57dbde
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,19 @@ export default function initUtils() {
},

calculatePercentage(scrollTop, scrollHeight, clientHeight) {
return Math.round((scrollTop / (scrollHeight - clientHeight)) * 100);
let percentageValue = Math.round(
(scrollTop / (scrollHeight - clientHeight)) * 100,
);
if (
isNaN(percentageValue) ||
percentageValue < 0 ||
!isFinite(percentageValue)
) {
percentageValue = 0;
} else if (percentageValue > 100) {
percentageValue = 100;
}
return percentageValue;
},

// register window scroll event
Expand Down

0 comments on commit c57dbde

Please sign in to comment.