Skip to content

Instantly share code, notes, and snippets.

@alizhdanov
Created August 21, 2016 20:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alizhdanov/53c2fbab17c9d7dd334607686d117a2a to your computer and use it in GitHub Desktop.
Save alizhdanov/53c2fbab17c9d7dd334607686d117a2a to your computer and use it in GitHub Desktop.
pure javascript scrollTop function
function scrollTo(element, to, duration) {
if (duration < 0) return;
var difference = to - element.scrollTop;
var perTick = difference / duration * 2;
setTimeout(function() {
element.scrollTop = element.scrollTop + perTick;
scrollTo(element, to, duration - 2);
}, 10);
}
document.querySelector('.js-top-button').onclick = function () {
scrollTo(document.body, 0, 100);
};
@jt3k
Copy link

jt3k commented May 4, 2018

rename plz -- scrollTop.js -> scrollTo.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment