From 3b070307bb171074e0477cc7bbcca8a8443d9795 Mon Sep 17 00:00:00 2001 From: Athou Date: Sun, 7 Apr 2013 07:08:59 +0200 Subject: [PATCH] scroll only if needed --- src/main/webapp/js/directives.js | 32 +++++++++++++++-------- src/main/webapp/templates/feeds.view.html | 4 +-- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/webapp/js/directives.js b/src/main/webapp/js/directives.js index a031d587..7737790f 100644 --- a/src/main/webapp/js/directives.js +++ b/src/main/webapp/js/directives.js @@ -11,21 +11,31 @@ app.directive('ngBlur', function() { }; }); -module.directive('scrollTo', function() { +module.directive('scrollTo', function($timeout) { return { restrict : 'A', - controller : function($scope, $element, $attrs) { - - }, link : function(scope, element, attrs) { scope.$watch(attrs.scrollTo, function(value) { - if (value) { - var offset = parseInt(attrs.scrollToOffset, 10); - var scrollTop = $(element).offset().top + offset; - $('html, body').animate({ - scrollTop : scrollTop - }, 0); - } + if (!value) + return; + $timeout(function() { + var docTop = $(window).scrollTop(); + var docBottom = docTop + $(window).height(); + + var elemTop = $(element).offset().top; + var elemBottom = elemTop + $(element).height(); + + if ((elemTop > docTop) && (elemBottom < docBottom)) { + // element is entirely visible + return; + } else { + var offset = parseInt(attrs.scrollToOffset, 10); + var scrollTop = $(element).offset().top + offset; + $('html, body').animate({ + scrollTop : scrollTop + }, 0); + } + }); }); } }; diff --git a/src/main/webapp/templates/feeds.view.html b/src/main/webapp/templates/feeds.view.html index c9844f70..2469732e 100644 --- a/src/main/webapp/templates/feeds.view.html +++ b/src/main/webapp/templates/feeds.view.html @@ -5,8 +5,8 @@
-
- + {{entry.feedName}}