Last active
August 29, 2015 14:17
-
-
Save alpercitak/21579538220cca80e51c to your computer and use it in GitHub Desktop.
angularjs infinite scroll directive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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