diff --git a/src/scrollglue.js b/src/scrollglue.js index 48c1788..fd23bd5 100644 --- a/src/scrollglue.js +++ b/src/scrollglue.js @@ -1,6 +1,6 @@ + (function(angular, undefined){ 'use strict'; - angular.module('luegg.directives', []) .directive('scrollGlue', ['$parse', function($parse){ function unboundState(initValue){ @@ -58,20 +58,41 @@ link: function(scope, $el, attrs){ var el = $el[0], activationState = createActivationState(attrs.scrollGlue, scope); - + var scrollParent = false; + var height = 0; function scrollToBottom(){ + if (scrollParent) { + if (el.parentElement.scrollHeight > 0) { + el.parentElement.scrollTop = height; + } else { + document.body.scrollTop = height; + } + } else { el.scrollTop = el.scrollHeight; + } } function onScopeChanges(scope){ - if(activationState.getValue() && !shouldActivateAutoScroll()){ + if(activationState.getValue() && shouldActivateAutoScroll()){ scrollToBottom(); } } function shouldActivateAutoScroll(){ // + 1 catches off by one errors in chrome - return el.scrollTop + el.clientHeight + 1 >= el.scrollHeight; + var result = el.scrollTop + el.offsetHeight + 1 <= el.scrollHeight; + if (!result) { + var scrollHeight = el.parentElement.scrollHeight; + if (scrollHeight === 0) { + scrollHeight = document.body.scrollHeight; + } + height = scrollHeight - window.innerHeight; + result = height !== 0; + scrollParent = true; + } else { + scrollParent = false; + } + return result; } function onScroll(){ @@ -83,4 +104,4 @@ } }; }]); -}(angular)); +}(angular)); \ No newline at end of file