Skip to content

Instantly share code, notes, and snippets.

@theohogberg
Created October 27, 2016 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theohogberg/1a29b884cef38f839d75354d735413cf to your computer and use it in GitHub Desktop.
Save theohogberg/1a29b884cef38f839d75354d735413cf to your computer and use it in GitHub Desktop.
import angular = require('angular');
function OnScrollToEndDirective($window, $document) {
'use strict';
return {
restrict: 'A',
link: link,
};
function link(scope, element, attrs) {
angular.element($window).bind('scroll', scrollHandler);
element.on('$destroy', function() {
angular.element($window).unbind('scroll', scrollHandler);
});
let above = true;
function scrollHandler() {
if (above && $document[0].body.scrollHeight - $window.scrollY - $window.innerHeight < 500) {
above = false;
scope.$apply(function() {
scope.$eval(attrs.onScrollToEnd);
});
} else {
above = true;
}
}
}
}
OnScrollToEndDirective.$inject = ['$window', '$document'];
export = OnScrollToEndDirective;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment