Last active
May 5, 2019 15:07
-
-
Save tarun-dugar/e80b44b990fa5f3bb827ef6f9472a768 to your computer and use it in GitHub Desktop.
Get total scroll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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