Skip to content

Instantly share code, notes, and snippets.

@snowleo208
Last active September 11, 2018 14:43
Show Gist options
  • Save snowleo208/6bd036b046f810e7fe52ebb8e52c7ed6 to your computer and use it in GitHub Desktop.
Save snowleo208/6bd036b046f810e7fe52ebb8e52c7ed6 to your computer and use it in GitHub Desktop.
// if target is 0 (back to top),
// the position is:
// current pos + (current pos * percentage of duration)
// if target > 0 (not back to top),
// the positon is current pos + (target pos * percentage of duration)
pos = (target === 0)? (firstPos - (firstPos * progress)) : (firstPos + (target * progress));
window.scrollTo(0, pos);
if(target !== 0 && pos >= (firstPos + target) || target === 0 && pos <= 0) {
cancelAnimationFrame(start);
if(element) { element.focus(); }
pos = 0;
} else {
window.requestAnimationFrame(showAnimation);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment