Skip to content

Instantly share code, notes, and snippets.

@rootux
Last active April 6, 2017 19:57
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 rootux/cbf99fb0931d24c563147cd0c3cac87a to your computer and use it in GitHub Desktop.
Save rootux/cbf99fb0931d24c563147cd0c3cac87a to your computer and use it in GitHub Desktop.
Angular 1.5 - Calls a method when user scrolls to bottom
function OnScrollToBottom() {
'ngInject';
let spacing = 40;
return {
restrict: 'A',
link: function (scope, element, attrs) {
const fn = attrs.onScrollToBottom,
clientHeight = element[0].clientHeight;
element.on('scroll', function (e) {
const el = e.target;
if ((el.scrollHeight - el.scrollTop) <= clientHeight + spacing) {
scope.$apply(fn);
}
});
}
};
}
export default OnScrollToBottom;
@rootux
Copy link
Author

rootux commented Apr 6, 2017

You can call it like so:
<div class='viewport' on-scroll-to-bottom="$ctrl.loadMorePosts()">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment