Last active
December 14, 2017 10:28
-
-
Save souporserious/a861018eab7c7fa9913c9eda309bd163 to your computer and use it in GitHub Desktop.
determine current scroll progress of an element
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
function createScrollFrame({ | |
scrollPosition = window.pageYOffset, | |
triggerSize = window.innerHeight, | |
triggerOrigin = 0, | |
startElement, | |
startOrigin = 0, | |
startOffset = 0, | |
endElement, | |
endOrigin = 1, | |
endOffset = 0, | |
debug = false | |
}) { | |
const startRect = startElement.getBoundingClientRect(); | |
const endRect = endElement ? endElement.getBoundingClientRect() : startRect; | |
const startPosition = | |
startRect.top + | |
startRect.height * startOrigin + | |
startOffset + | |
scrollPosition; | |
const endPosition = | |
endRect.top + endRect.height * endOrigin + endOffset + scrollPosition; | |
const trigger = scrollPosition + triggerSize * triggerOrigin; | |
return { | |
startPosition, | |
endPosition, | |
startDifference: startRect.top - endRect.top, | |
endDifference: startRect.bottom - endRect.bottom, | |
progress: (trigger - startPosition) / (endPosition - startPosition) | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment