scroll only if needed

This commit is contained in:
Athou
2013-04-07 07:08:59 +02:00
parent 3691dcd98f
commit 3b070307bb
2 changed files with 23 additions and 13 deletions

View File

@@ -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);
}
});
});
}
};