Skip to content

Instantly share code, notes, and snippets.

@tbusser
Created March 19, 2014 15:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tbusser/9643528 to your computer and use it in GitHub Desktop.
Save tbusser/9643528 to your computer and use it in GitHub Desktop.
/**
* 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