Skip to content

Commit

Permalink
explose scrollTo
Browse files Browse the repository at this point in the history
  • Loading branch information
schlosser committed Feb 4, 2016
1 parent f0dae2d commit da00c36
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
72 changes: 36 additions & 36 deletions src/pageanimation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,6 @@
}
}

/**
* Scroll to the top of the page, for scrollDuration ms, calling cb when done.
*
* @param {int} offset - the offset from the top of the document to scroll to
* @param {int} scrollDuration - how long the scroll should take, in ms
* @param {function} cb - callback to call when the scroll is complete
*/
function _scrollTo(offset, scrollDuration, cb) {
var scrollHeight = window.scrollY;
var scrollStep = Math.PI / (scrollDuration / 15);
var cosParameter = (scrollHeight - offset) / 2;
var scrollCount = 0;
var cosArgument;
var scrollMargin;
requestAnimationFrame(step);

function step() {
setTimeout(function() {
if (Math.abs(window.scrollY - offset) > 5) {
requestAnimationFrame(step);
scrollCount = scrollCount + 1;
cosArgument = Math.max(0, Math.min(Math.PI, scrollCount * scrollStep));
scrollMargin = cosParameter - cosParameter * Math.cos(cosArgument);
window.scrollTo(0, (scrollHeight - scrollMargin));
} else {
window.scrollTo(0, offset);
cb();
}
}, 15);
}
}

/**
* Get the anchor tag that triggered the event being clicked.
*
Expand Down Expand Up @@ -235,6 +203,39 @@
}
};

/**
* Scroll to the top of the page, for scrollDuration ms, calling cb when done.
*
* @param {int} offset - the offset from the top of the document to scroll to
* @param {int} scrollDuration - how long the scroll should take, in ms
* @param {function} cb - callback to call when the scroll is complete
*/
PageAnimation.scrollTo = function(offset, scrollDuration, cb) {
cb = cb || function() {};
var scrollHeight = window.scrollY;
var scrollStep = Math.PI / (scrollDuration / 15);
var cosParameter = (scrollHeight - offset) / 2;
var scrollCount = 0;
var cosArgument;
var scrollMargin;
requestAnimationFrame(step);

function step() {
setTimeout(function() {
if (Math.abs(window.scrollY - offset) > 5) {
requestAnimationFrame(step);
scrollCount = scrollCount + 1;
cosArgument = Math.max(0, Math.min(Math.PI, scrollCount * scrollStep));
scrollMargin = cosParameter - cosParameter * Math.cos(cosArgument);
window.scrollTo(0, (scrollHeight - scrollMargin));
} else {
window.scrollTo(0, offset);
cb();
}
}, 15);
}
};

/**
* Called when the animation is complete.
*
Expand All @@ -257,7 +258,7 @@
}.bind(this);

if (animation.shouldScroll && animation.scrollTiming === 'after') {
_scrollTo(this.cb.computeScrollOffset(animation), 200, followLink);
PageAnimation.scrollTo(this.cb.computeScrollOffset(animation), 200, followLink);
} else {
followLink();
}
Expand Down Expand Up @@ -289,10 +290,10 @@
}.bind(this);

if (animation.shouldScroll && animation.scrollTiming === 'before') {
_scrollTo(this.cb.computeScrollOffset(animation), 200, startAnimation);
PageAnimation.scrollTo(this.cb.computeScrollOffset(animation), 200, startAnimation);
} else if (animation.shouldScroll && animation.scrollTiming === 'during') {
setTimeout(startAnimation, 0);
_scrollTo(this.cb.computeScrollOffset(animation), 200);
PageAnimation.scrollTo(this.cb.computeScrollOffset(animation), 200);
} else {
startAnimation();
}
Expand Down Expand Up @@ -328,7 +329,6 @@
}
};


if (typeof define === 'function' && define.amd) {
define(PageAnimation);
} else if (typeof module !== 'undefined' && module.exports) {
Expand Down
2 changes: 1 addition & 1 deletion src/pageanimation.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da00c36

Please sign in to comment.