Created
November 5, 2016 13:10
-
-
Save adam-s/e54c4357648ed0ca6bee24eb8573b0cd to your computer and use it in GitHub Desktop.
This file contains 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
// 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