Skip to content

Instantly share code, notes, and snippets.

@dverbovyi
Last active November 21, 2017 15:30
Show Gist options
  • Save dverbovyi/f501f2e7fa13952bb8b25927975aa43b to your computer and use it in GitHub Desktop.
Save dverbovyi/f501f2e7fa13952bb8b25927975aa43b to your computer and use it in GitHub Desktop.
AngularJS (v1) simple directive which fire the listener on scroll finish
<div scroll-end="loadMore()">
(function () {
'use strict';
angular
.module('app', [])
.directive('scrollEnd', scrollEndDirective);
function scrollEndDirective() {
return {
restrict: 'A',
link: link
};
function link(scope, element, attrs) {
var visibleHeight = element.height();
var threshold = 100;
//TODO: add threshold
element.bind('scroll', _onScrollHandler);
function _onScrollHandler() {
var scrollableHeight = element.prop('scrollHeight');
var hiddenContentHeight = scrollableHeight - visibleHeight;
if (hiddenContentHeight - element.scrollTop() <= threshold) {
scope.$apply(attrs.scrollEnd);
}
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment