Last active
June 14, 2017 10:01
-
-
Save amitparrikar/bbd52f7fe2c6528fc37974e197ccc84b to your computer and use it in GitHub Desktop.
An AngularJS directive called InfiniteScroll; Infinite Scrolling mechanism is used in lazy loading. It gets a callback handler which then gets called when scrolling has reached to the bottom of the element.
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
| /** | |
| * InfiniteScroll is a AngularJS directive. | |
| * This directive calls your callback function when scrolling of an element is reached to the bottom. | |
| */ | |
| 'use strict'; | |
| angular.module('InfiniteScroll', []).directive('infiniteScroll', function () { | |
| return { | |
| restrict: 'A', | |
| scope: { | |
| callback: '&infiniteScroll' | |
| }, | |
| link: function($scope, iElem, iAttr){ | |
| /** | |
| * Scroll callback event handler. | |
| * @param {[type]} e [description] | |
| * @return {[type]} [description] | |
| */ | |
| var _scrollHandler = function (e) { | |
| if((e.target.scrollTop + e.target.clientHeight) >= e.target.scrollHeight){ | |
| $scope.callback({$event: e}); | |
| } | |
| }; | |
| /** | |
| * Attach the Scroll event to the element. | |
| */ | |
| iElem.on('scroll', _scrollHandler); | |
| /** | |
| * Unbind the scroll event when the scope is destroyed. | |
| * @return {[type]} [description] | |
| */ | |
| $scope.$on('$destroy', function () { | |
| iElem.off('scroll', _scrollHandler); | |
| }); | |
| } | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment