Created
March 19, 2014 15:00
-
-
Save tbusser/9643528 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
/** | |
* This method is used to scroll a bit closer to the top of the window. As long as the top | |
* hasn't been reached the method will call itself at the next animation frame. | |
*/ | |
_animationLoop: function() { | |
var self = this; | |
// Perform a step of the animation to go to the top of the window | |
window.scrollTo(0, this._easeOutCubic(this._iteration, this._scrollPosition, -this._scrollPosition, this._maxIterations)); | |
// Increase the iteration counter | |
this._iteration++; | |
// As long as we haven't gone through the max number of iterations we will have to schedule | |
// the next part of the animation | |
if (this._iteration <= this._maxIterations) { | |
// We're not there yet, request a new animation frame to move a little closer to the top | |
requestAnimationFrame(function() { | |
self._animationLoop(); | |
}); | |
} | |
}, | |
/** | |
* The click handler for the element which will initiate the scroll to top. | |
*/ | |
_buttonClickHandler: function(event) { | |
// We need to remember at which scroll position the document is before we start the | |
// animation. We need this value as a constant during the animation. | |
this._scrollPosition = this._getScrollPosition(); | |
// Reset the iteration count | |
this._iteration = 0; | |
// Start the animation | |
this._animationLoop(); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment