Skip to content

Instantly share code, notes, and snippets.

@alpercitak
Last active August 29, 2015 14:17
Show Gist options
  • Save alpercitak/21579538220cca80e51c to your computer and use it in GitHub Desktop.
Save alpercitak/21579538220cca80e51c to your computer and use it in GitHub Desktop.
angularjs infinite scroll directive
var mod = angular.module('ng-infinite', []);
mod.directive('ngInfinite', [ '$window', '$timeout', function($window, $timeout) {
return {
link : function(scope, elem, attrs) {
scope.$on('$destroy', function() {
return container.off('scroll', handler);
});
var calculate = function() {
var body = $('body');
var clientHeight = document.documentElement.clientHeight;
var scrollTop = body.scrollTop();
var height = body.height();
var marginTop = parseInt(body.css('margin-top'));
var paddingTop = parseInt(body.css('padding-top'));
if (clientHeight + scrollTop - height - marginTop - paddingTop === 0) {
return true;
} else if (body.height() <= clientHeight) {
return true;
}
return false;
}
var predict = function() {
if (calculate() && !scope.$$phase) {
scope.$apply(attrs.ngInfinite);
}
}
return setInterval(function() {
predict();
}, 1000);
}
}
} ]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment