Skip to content

Commit

Permalink
feat: added getScrolledPercentages
Browse files Browse the repository at this point in the history
  • Loading branch information
teclone committed Feb 28, 2020
1 parent 9cf0b71 commit 9956880
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1047,3 +1047,26 @@ export const getScrollPositions = (elem?: HTMLElement | null) => {

return result;
};

/**
* returns the current scroll percentages
*/
export const getScrolledPercentages = (elem?: HTMLElement | null) => {
const result = {
x: 0,
y: 0,
};

const amountToScroll = (scrollSize: number, clientSize: number) => {
return scrollSize - clientSize || 1;
};

const scrollSize = getScrollSize(elem);
const scrollPositions = getScrollPositions(elem);
const clientSize = getClientSize(elem);

result.x = scrollPositions.left / amountToScroll(scrollSize.width, clientSize.width);
result.y = scrollPositions.top / amountToScroll(scrollSize.height, clientSize.height);

return result;
};

0 comments on commit 9956880

Please sign in to comment.