-
-
Save arsonus/bcecf1c0c20354589020eee7350cb707 to your computer and use it in GitHub Desktop.
pure javascript scrollTop function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment