Skip to content

Instantly share code, notes, and snippets.

@fermartz
Created December 12, 2019 14:18
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 fermartz/c16e781d4bc1ec7c5e0c4b62bec891ed to your computer and use it in GitHub Desktop.
Save fermartz/c16e781d4bc1ec7c5e0c4b62bec891ed to your computer and use it in GitHub Desktop.
const scrollTo = (goTo, offset = 0, goToNumber = 0, time = 600) => {
var target = document.querySelectorAll(goTo)[goToNumber]
var targetPosition = target.offsetTop - offset
var startPosition = window.scrollY
var distance = targetPosition - startPosition
var duration = time
var startTime = null
function animationScroll(currentTime) {
if (startTime === null) startTime = currentTime
var timeElapsed = currentTime - startTime
var run = ease(timeElapsed, startPosition, distance, duration)
window.scrollTo(0, run)
if (timeElapsed < duration) requestAnimationFrame(animationScroll)
}
function ease(t, b, c, d) {
t /= d / 2
if (t < 1) return (c / 2) * t * t + b
t--
return (-c / 2) * (t * (t - 2) - 1) + b
}
requestAnimationFrame(animationScroll)
}
export default scrollTo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment