From bd9083361ef4a46d5ca018d00012fc2367adada3 Mon Sep 17 00:00:00 2001 From: Zam-mbpr Date: Wed, 4 Feb 2015 17:51:22 +0800 Subject: [PATCH 1/2] fix problem: if there is no height property then no scroll. --- src/scrollglue.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/scrollglue.js b/src/scrollglue.js index 48c1788..13b16dc 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){ @@ -40,6 +40,7 @@ } function createActivationState(attr, scope){ + console.log(attr !== "") if(attr !== ""){ var getter = $parse(attr); if(getter.assign !== undefined){ @@ -58,20 +59,32 @@ link: function(scope, $el, attrs){ var el = $el[0], activationState = createActivationState(attrs.scrollGlue, scope); - + var scrollParent = false; function scrollToBottom(){ + if (scrollParent) { + var height = el.parentElement.scrollHeight - window.innerHeight; + el.parentElement.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) { + result = (el.parentElement.scrollHeight - window.innerHeight) !== 0; + scrollParent = true; + } else { + scrollParent = false; + } + return result; } function onScroll(){ @@ -83,4 +96,4 @@ } }; }]); -}(angular)); +}(angular)); \ No newline at end of file From 0426b7d28f702cff848a6475d071d7d9b3647c26 Mon Sep 17 00:00:00 2001 From: Zam-mbpr Date: Wed, 4 Feb 2015 21:10:18 +0800 Subject: [PATCH 2/2] let it work in android webview either --- src/scrollglue.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/scrollglue.js b/src/scrollglue.js index 13b16dc..fd23bd5 100644 --- a/src/scrollglue.js +++ b/src/scrollglue.js @@ -40,7 +40,6 @@ } function createActivationState(attr, scope){ - console.log(attr !== "") if(attr !== ""){ var getter = $parse(attr); if(getter.assign !== undefined){ @@ -60,10 +59,14 @@ var el = $el[0], activationState = createActivationState(attrs.scrollGlue, scope); var scrollParent = false; + var height = 0; function scrollToBottom(){ if (scrollParent) { - var height = el.parentElement.scrollHeight - window.innerHeight; - el.parentElement.scrollTop = height; + if (el.parentElement.scrollHeight > 0) { + el.parentElement.scrollTop = height; + } else { + document.body.scrollTop = height; + } } else { el.scrollTop = el.scrollHeight; } @@ -79,7 +82,12 @@ // + 1 catches off by one errors in chrome var result = el.scrollTop + el.offsetHeight + 1 <= el.scrollHeight; if (!result) { - result = (el.parentElement.scrollHeight - window.innerHeight) !== 0; + var scrollHeight = el.parentElement.scrollHeight; + if (scrollHeight === 0) { + scrollHeight = document.body.scrollHeight; + } + height = scrollHeight - window.innerHeight; + result = height !== 0; scrollParent = true; } else { scrollParent = false;