From 740b1921fedc8629684a3123ea0dbdd04d709809 Mon Sep 17 00:00:00 2001 From: Yanin Tuamsuk Date: Thu, 30 Jun 2016 15:58:42 +0700 Subject: [PATCH 1/3] add option for use with horizontal scrolling --- animatedScrollTo.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/animatedScrollTo.js b/animatedScrollTo.js index bc7d0cb..550a3a6 100644 --- a/animatedScrollTo.js +++ b/animatedScrollTo.js @@ -8,8 +8,12 @@ return -c/2 * (t*(t-2) - 1) + b; }; - var animatedScrollTo = function (element, to, duration, callback) { - var start = element.scrollTop, + var animatedScrollTo = function (element, to, duration, scrollLeft, callback) { + + var direction = 'scrollTop' + if (scrollLeft) direction = 'scrollLeft' + + var start = element[direction], change = to - start, animationStart = +new Date(); var animating = true; @@ -23,18 +27,18 @@ var now = +new Date(); var val = Math.floor(easeInOutQuad(now - animationStart, start, change, duration)); if (lastpos) { - if (lastpos === element.scrollTop) { + if (lastpos === element[direction]) { lastpos = val; - element.scrollTop = val; + element[direction] = val; } else { animating = false; } } else { lastpos = val; - element.scrollTop = val; + element[direction] = val; } if (now > animationStart + duration) { - element.scrollTop = to; + element[direction] = to; animating = false; if (callback) { callback(); } } From f8fd12d247e4046fcab9fab44e76ae87f51ba0ce Mon Sep 17 00:00:00 2001 From: Yanin Tuamsuk Date: Thu, 30 Jun 2016 16:00:05 +0700 Subject: [PATCH 2/3] =?UTF-8?q?fix=20callback=20doesn=E2=80=99t=20call=20i?= =?UTF-8?q?f=20target=20scroll=20exceeds=20scrollWidth?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- animatedScrollTo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/animatedScrollTo.js b/animatedScrollTo.js index 550a3a6..cc221ec 100644 --- a/animatedScrollTo.js +++ b/animatedScrollTo.js @@ -21,6 +21,7 @@ var animateScroll = function() { if (!animating) { + if (callback) { callback(); } return; } requestAnimFrame(animateScroll); @@ -40,7 +41,6 @@ if (now > animationStart + duration) { element[direction] = to; animating = false; - if (callback) { callback(); } } }; requestAnimFrame(animateScroll); From bb48628afbe52e86b02081ff43a8effde4d740c0 Mon Sep 17 00:00:00 2001 From: Yanin Tuamsuk Date: Thu, 30 Jun 2016 16:12:53 +0700 Subject: [PATCH 3/3] add docs for horizontal scrolling --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 512c497..1e855d5 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ button.addEventListener('click', function () { document.body, // element to scroll with (most of times you want to scroll with whole ) 0, // target scrollY (0 means top of the page) 10000, // duration in ms + true, // horizontal scrolling? function() { // callback function that runs after the animation (optional) console.log('done!') }