-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngStickTo.js
28 lines (25 loc) · 972 Bytes
/
ngStickTo.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
.directive('stick-to', function ($window) {
return {
restrict: 'A',
scope: {
contain: '@',
offset: '@'
},
link: function (scope, element, attr) {
//vars
var scrollElm = $(element);
var parent = $(element).parent('div');
var parentOffset = parent.offset();
//set Parent relative
parent.css('position', 'relative');
//bind scroll
angular.element($window).bind("scroll", function () {
//check if in viewport
scope.inView = (scope.contain == 'true' ? this.pageYOffset >= parentOffset.top : true);
//set css
scrollElm.css('position', (scope.inView ? "absolute" : "relative"));
scrollElm.css('top', (scope.inView ? this.pageYOffset - parentOffset.top + parseInt(scope.offset) : parseInt(scope.offset)));
});
}
}
})