Created
December 29, 2015 14:02
-
-
Save derick-montague/84008c248fd2fb19ec60 to your computer and use it in GitHub Desktop.
Simple and small vanilla js script for animating scroll to top
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() { | |
'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