Skip to content

Commit bfca70a

Browse files
authored
Add safezone to scroll end detection to prevent edge cases when scrolling to the end would not detect end (#201)
1 parent 12cfc81 commit bfca70a

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/components/reader/pager/HorizontalPager.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ const findCurrentPageIndex = (wrapper: HTMLDivElement): number => {
2020
return -1;
2121
};
2222

23-
const isAtEnd = () => window.innerWidth + window.scrollX >= document.body.scrollWidth;
23+
const SCROLL_SAFE_ZONE = 5; // px
24+
const isAtEnd = () => {
25+
const visibleEnd = window.innerWidth + window.scrollX;
26+
// SCROLL_SAFE_ZONE is here for special cases when window might be .5px shorter
27+
// and math just dont add up correctly
28+
return visibleEnd >= document.body.scrollWidth - SCROLL_SAFE_ZONE;
29+
};
2430
const isAtStart = () => window.scrollX <= 0;
2531

2632
export default function HorizontalPager(props: IReaderProps) {

src/components/reader/pager/VerticalPager.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ const findCurrentPageIndex = (wrapper: HTMLDivElement): number => {
2121
};
2222

2323
// TODO: make configurable?
24+
const SCROLL_SAFE_ZONE = 5; // px
2425
const SCROLL_OFFSET = 0.95;
2526
const SCROLL_BEHAVIOR: ScrollBehavior = 'smooth';
2627

27-
const isAtBottom = () => window.innerHeight + window.scrollY >= document.body.offsetHeight;
28+
const isAtBottom = () => {
29+
const visibleBottom = window.innerHeight + window.scrollY;
30+
// SCROLL_SAFE_ZONE is here for special cases when window might be .5px shorter
31+
// and math just dont add up correctly
32+
return visibleBottom >= document.body.offsetHeight - SCROLL_SAFE_ZONE;
33+
};
2834
const isAtTop = () => window.scrollY <= 0;
2935

3036
export default function VerticalPager(props: IReaderProps) {

0 commit comments

Comments
 (0)