Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Last active December 22, 2015 09:38
Show Gist options
  • Save clouddueling/6452758 to your computer and use it in GitHub Desktop.
Save clouddueling/6452758 to your computer and use it in GitHub Desktop.
infinite scroll for fixed positioned divs angularjs
var mod;
mod = angular.module('infinite-scroll', []);
mod.directive('infiniteScroll', [
'$rootScope', '$timeout', function($rootScope, $timeout) {
return {
link: function(scope, elem, attrs) {
var checkWhenEnabled, handler, scrollDistance, scrollEnabled;
$parent = angular.element(attrs.infiniteScrollParent);
$child = angular.element(attrs.infiniteScrollChild);
scrollDistance = 0;
if (attrs.infiniteScrollDistance != null) {
scope.$watch(attrs.infiniteScrollDistance, function(value) {
return scrollDistance = parseInt(value, 10);
});
}
scrollEnabled = true;
checkWhenEnabled = false;
if (attrs.infiniteScrollDisabled != null) {
scope.$watch(attrs.infiniteScrollDisabled, function(value) {
scrollEnabled = !value;
if (scrollEnabled && checkWhenEnabled) {
checkWhenEnabled = false;
return handler();
}
});
}
handler = function() {
var elementBottom, remaining, shouldScroll, windowBottom;
windowBottom = $child.height() - $parent.scrollTop() - $parent.height();
shouldScroll = windowBottom < scrollDistance;
if (shouldScroll && scrollEnabled) {
if ($rootScope.$$phase) {
return scope.$eval(attrs.infiniteScroll);
} else {
return scope.$apply(attrs.infiniteScroll);
}
} else if (shouldScroll) {
return checkWhenEnabled = true;
}
};
$parent.on('scroll', handler);
scope.$on('$destroy', function() {
return $window.off('scroll', handler);
});
return $timeout((function() {
if (attrs.infiniteScrollImmediateCheck) {
if (scope.$eval(attrs.infiniteScrollImmediateCheck)) {
return handler();
}
} else {
return handler();
}
}), 0);
}
};
}
]);
<div class='right-column-wrapper'>
<div class='right-column-inner'>
<div
infinite-scroll="nextConversations()"
infinite-scroll-disabled="busy"
infinite-scroll-distance="300"
infinite-scroll-parent=".right-column-wrapper"
infinite-scroll-child=".right-column-inner">
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment