forked from pnu/angular-when-on-screen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-when-on-screen.js
executable file
·38 lines (34 loc) · 1.34 KB
/
angular-when-on-screen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(function(){
var module = angular.module("whenOnScreen", []);
module.directive("whenOnScreenScroller", function() { return {
restrict: "A",
controller: ['$element',function($element) {
this.el = $element;
}]
}; } );
module.directive("whenOnScreen", [ "$window", function($window) { return {
scope: { whenOnScreen: '&' },
require: '?^whenOnScreenScroller',
restrict: "A",
link: function(scope, el, attr, scroller) {
var inViewPrev = false;
var onScroll = function() {
var scEl = scroller ? scroller.el[0] : undefined;
var scElYPos = scEl ? scEl.offsetTop + scEl.clientTop : 0;
var scElHeight = scEl ? scEl.clientHeight : $window.innerHeight;
var scrollY = scEl ? scEl.scrollTop : $window.scrollY;
var elTopY = el[0].offsetTop - scElYPos - scrollY;
var elBottomY = elTopY + el[0].scrollHeight;
var inView = elBottomY > 0 && elTopY < scElHeight;
if (inView && !inViewPrev) scope.whenOnScreen();
inViewPrev = inView;
};
var scrollerEl = scroller ? scroller.el : angular.element($window);
scrollerEl.on("scroll", onScroll);
setTimeout(onScroll, 200);
scope.$on("$destroy", function() {
scrollerEl.off("scroll", onScroll);
});
}
}; } ]);
})();