diff --git a/README.md b/README.md index a3b6dd7..9fd6387 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ This is a simple function for animating scroll. - If user scrolls while animation is running, scroll animation would be immediately canceled. - Use as a single script or through bower. +## Browser support + +- If you want full browser support include this library: https://github.com/mathiasbynens/document.scrollingElement + ## Example usage ```javascript diff --git a/animatedScrollTo.js b/animatedScrollTo.js index bc7d0cb..b045751 100644 --- a/animatedScrollTo.js +++ b/animatedScrollTo.js @@ -10,8 +10,8 @@ var animatedScrollTo = function (element, to, duration, callback) { var start = element.scrollTop, - change = to - start, - animationStart = +new Date(); + change = to - start, + animationStart = +new Date(); var animating = true; var lastpos = null; @@ -33,13 +33,23 @@ lastpos = val; element.scrollTop = val; } - if (now > animationStart + duration) { + if (now > animationStart + duration || isBottomPage(element)) { element.scrollTop = to; animating = false; if (callback) { callback(); } } }; + requestAnimFrame(animateScroll); + + var isBottomPage = function(element){ + var scrollTop = element.scrollTop; + var scrolledToBottom = (scrollTop + window.innerHeight) >= element.scrollHeight; + + if(scrolledToBottom){ + return true; + } + } }; if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { @@ -47,4 +57,4 @@ } else { window.animatedScrollTo = animatedScrollTo; } -})(window); +})(window); \ No newline at end of file diff --git a/package.json b/package.json index c4b1ba1..aa83faf 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "animated-scrollto", + "name": "animated-scrollto-gijs", "version": "1.1.0", "description": "Animated scrolling without any dependency on libraries", "main": "animatedScrollTo.js",