Skip to content

Instantly share code, notes, and snippets.

@adam-s
Created November 5, 2016 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adam-s/e54c4357648ed0ca6bee24eb8573b0cd to your computer and use it in GitHub Desktop.
Save adam-s/e54c4357648ed0ca6bee24eb8573b0cd to your computer and use it in GitHub Desktop.
// http://stackoverflow.com/questions/21474678/scrolltop-animation-without-jquery
function scrollToTop() {
var deferred = $q.defer();
var content = document.getElementById('content');
var scrollDuration = 1200 * ((content.scrollTop / content.offsetHeight));
var cosParameter = content.scrollTop / 2,
scrollCount = 0,
oldTimestamp = performance.now();
function step (newTimestamp) {
scrollCount += Math.PI / (scrollDuration / (newTimestamp - oldTimestamp));
if (scrollCount >= Math.PI) content.scrollTop = 0;
if (content.scrollTop === 0) {
deferred.resolve();
} else {
content.scrollTop =Math.round(cosParameter + cosParameter * Math.cos(scrollCount));
oldTimestamp = newTimestamp;
window.requestAnimationFrame(step);
}
}
window.requestAnimationFrame(step);
return deferred.promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment