Skip to content

Instantly share code, notes, and snippets.

@jumbojett
Last active November 21, 2018 12:05
Show Gist options
  • Save jumbojett/5967535 to your computer and use it in GitHub Desktop.
Save jumbojett/5967535 to your computer and use it in GitHub Desktop.
AngularJS: Infinite Scrolling Directive
<div on-scroll="loadMore()">
<!-- content -->
</div>
/*
* Improved version of http://jsfiddle.net/vojtajina/U7Bz9/‎
*/
angular.module('scroll', []).directive('onScroll', function () {
return function (scope, elm, attr) {
angular.element(window).bind('scroll', function () {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
scope.$apply(attr.onScroll);
}
});
};
});
@korxz
Copy link

korxz commented Nov 21, 2018

Hello,
I have been using the "old" code for infinity scroll and now I found your improvments. The old code was buggy and it failed few times. But with your code I didn't liked that it loaded all data after few scrolls. I modified the code a bit and I think this works better then old version, but loads data more slowly. Maybe this snippet will have somebody.

angular.module('scroll', []).directive('onScroll', () => {
        return (scope, elm, attr) => {
            elm.bind('scroll', () => {
                if ((window.innerHeight + elm[0].scrollTop) >= elm[0].scrollHeight) {
                    scope.$apply(attr.onScroll)
                }
            })
        }
    });

Best regards, Rok.

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