Skip to content

Instantly share code, notes, and snippets.

@tarun-dugar
Last active May 5, 2019 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarun-dugar/e80b44b990fa5f3bb827ef6f9472a768 to your computer and use it in GitHub Desktop.
Save tarun-dugar/e80b44b990fa5f3bb827ef6f9472a768 to your computer and use it in GitHub Desktop.
Get total scroll
// returns the total scroll amount in pixels
const getTotalScroll = ({
isWindow,
elementToScroll,
elementWidthProp,
initialScrollPosition,
scrollLengthProp
}) => {
let totalScroll;
if (isWindow) {
const documentElement = document.documentElement;
totalScroll = documentElement.offsetWidth
} else {
totalScroll = elementToScroll[scrollLengthProp] - elementToScroll[elementLengthProp];
}
return totalScroll - initialScrollPosition;
}
function smoothScroll(scrollParams) {
const elementToScroll = scrollParams.element;
const isWindow = elementToScroll === window;
const scrollDirectionProp = isWindow ? 'scrollX' : 'scrollLeft';
const elementWidthProp = isWindow ? 'innerWidth' : 'clientWidth';
const scrollLengthProp = 'scrollWidth';
const initialScrollPosition = elementToScroll[scrollDirectionProp];
let totalScroll = getTotalScroll({
isWindow,
elementToScroll,
elementWidthProp,
initialScrollPosition,
scrollLengthProp
});
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment