diff --git a/.gitignore b/.gitignore index 48a2e24..4d78832 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +node_modules components build diff --git a/example-container.html b/example-container.html new file mode 100644 index 0000000..ff6f050 --- /dev/null +++ b/example-container.html @@ -0,0 +1,66 @@ + + + scroll-to - smooth javascript window scrolling with requestAnimationFrame + + + +
+ + + + + + diff --git a/index.js b/index.js index 152e777..5384d6f 100644 --- a/index.js +++ b/index.js @@ -23,7 +23,7 @@ function scrollTo(x, y, options) { options = options || {}; // start position - var start = scroll(); + var start = scroll(options); // setup tween var tween = Tween(start) @@ -31,10 +31,17 @@ function scrollTo(x, y, options) { .to({ top: y, left: x }) .duration(options.duration || 1000); + var updateScroll = options.container + ? function (o) { + options.container.scrollTop = o.top | 0; + options.container.scrollLeft = o.left | 0; + } + : function (o) { + window.scrollTo(o.left | 0, o.top | 0); + } + // scroll - tween.update(function(o){ - window.scrollTo(o.left | 0, o.top | 0); - }); + tween.update(updateScroll); // handle end tween.on('end', function(){ @@ -48,7 +55,7 @@ function scrollTo(x, y, options) { } animate(); - + return tween; } @@ -59,8 +66,12 @@ function scrollTo(x, y, options) { * @api private */ -function scroll() { - var y = window.pageYOffset || document.documentElement.scrollTop; - var x = window.pageXOffset || document.documentElement.scrollLeft; +function scroll(options) { + var y = options.container + ? options.container.scrollTop + : window.pageYOffset || document.documentElement.scrollTop; + var x = options.container + ? options.container.scrollLeft + : window.pageXOffset || document.documentElement.scrollLeft; return { top: y, left: x }; }