Skip to content

Instantly share code, notes, and snippets.

@lmiller1990
Created December 7, 2019 08:45
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 lmiller1990/ab84baccccb33338c21528b96e525e00 to your computer and use it in GitHub Desktop.
Save lmiller1990/ab84baccccb33338c21528b96e525e00 to your computer and use it in GitHub Desktop.
const getScrollPercentage = (startMarker: HTMLElement, endMarker: HTMLElement): number => {
const offsetFromTop = getPosRelativeToBody(startMarker)
const total = getPosRelativeToBody(endMarker) - offsetFromTop - window.innerHeight
const progress = ((window.scrollY - offsetFromTop) / total)
return progress
}
const updateCircle = (circle: SVGCircleElement, progress: number): void => {
const circumference = circle.r.baseVal.value * 2 * Math.PI
// draw nothing
if (progress < 0) {
circle.style.strokeDashoffset = `${circumference}`
return
}
// draw the full circle
if (progress > 1) {
circle.style.strokeDashoffset = '0'
return
}
circle.style.strokeWidth = '4'
circle.style.strokeDasharray = `${circumference}`
const offset = circumference - (circumference * progress)
circle.style.strokeDashoffset = `${offset}`
}
const getPosRelativeToBody = (el: HTMLElement): number => {
return Math.abs(
document.documentElement.getBoundingClientRect().top - el.getBoundingClientRect().top,
);
}
export {
getScrollPercentage,
updateCircle,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment