Skip to content

Instantly share code, notes, and snippets.

@souporserious
Last active December 14, 2017 10:28
Show Gist options
  • Save souporserious/a861018eab7c7fa9913c9eda309bd163 to your computer and use it in GitHub Desktop.
Save souporserious/a861018eab7c7fa9913c9eda309bd163 to your computer and use it in GitHub Desktop.
determine current scroll progress of an element
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