Skip to content

Instantly share code, notes, and snippets.

@DKunin
Created December 24, 2015 15:05
Show Gist options
  • Select an option

  • Save DKunin/26126648da287d0f62d2 to your computer and use it in GitHub Desktop.

Select an option

Save DKunin/26126648da287d0f62d2 to your computer and use it in GitHub Desktop.
function scrollTo(element, to, duration) {
if (duration <= 0) {
return;
}
var difference = to - element.scrollTop;
var perTick = difference / duration * 10;
setTimeout(() => {
element.scrollTop = element.scrollTop + perTick;
if (element.scrollTop === to) {
return;
}
scrollTo(element, to, duration - 10);
}, 10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment