Last active
November 21, 2017 15:30
-
-
Save dverbovyi/f501f2e7fa13952bb8b25927975aa43b to your computer and use it in GitHub Desktop.
AngularJS (v1) simple directive which fire the listener on scroll finish
This file contains 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
<div scroll-end="loadMore()"> |
This file contains 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
(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