scroll-to
Version:
Smooth window scroll to position with requestAnimationFrame
67 lines (52 loc) • 1.05 kB
JavaScript
/**
* Module dependencies.
*/
var Tween = require('tween');
var raf = require('raf');
/**
* Expose `scrollTo`.
*/
module.exports = scrollTo;
/**
* Scroll to `(x, y)`.
*
* @param {Number} x
* @param {Number} y
* @api public
*/
function scrollTo(x, y, options) {
options = options || {};
// start position
var start = scroll();
// setup tween
var tween = Tween(start)
.ease(options.ease || 'out-circ')
.to({ top: y, left: x })
.duration(options.duration || 1000);
// scroll
tween.update(function(o){
window.scrollTo(o.left | 0, o.top | 0);
});
// handle end
tween.on('end', function(){
animate = function(){};
});
// animate
function animate() {
raf(animate);
tween.update();
}
animate();
return tween;
}
/**
* Return scroll position.
*
* @return {Object}
* @api private
*/
function scroll() {
var y = window.pageYOffset || document.documentElement.scrollTop;
var x = window.pageXOffset || document.documentElement.scrollLeft;
return { top: y, left: x };
}