Skip to content

Instantly share code, notes, and snippets.

@derick-montague
Created December 29, 2015 14:02
Show Gist options
  • Save derick-montague/84008c248fd2fb19ec60 to your computer and use it in GitHub Desktop.
Save derick-montague/84008c248fd2fb19ec60 to your computer and use it in GitHub Desktop.
Simple and small vanilla js script for animating scroll to top
(function() {
'use strict';
var toTopLink = document.querySelector('.js-scroll');
var scrollToTop = function(scrollDuration) {
var scrollStep = -window.scrollY / (scrollDuration / 15),
scrollInterval = setInterval(function() {
if ( window.scrollY !== 0 ) {
window.scrollBy( 0, scrollStep );
}
else {
clearInterval(scrollInterval);
}
},15);
};
// Click event listener for back to top button
if (toTopLink) {
toTopLink.addEventListener('click', function(e) {
e.preventDefault();
scrollToTop(500);
});
}
})();
// EaseInOut
// time < 0.5 ? 4 * time * time * time : (time - 1) * (2 * time - 2) * (2 * time - 2) + 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment