-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-sticky.js
124 lines (113 loc) · 4.16 KB
/
angular-sticky.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Created by DerekHu on 9/18/14.
*/
angular.module('dhu.sticky', [])
.directive('sticky', [function() {
return {
restrict: 'A',
scope: {
locate: '@stickyLocate',
at: '@stickyAt',
animation: '@stickyAnimation'
},
link: function(scope, element, attrs) {
var locate = scope.locate || 'top',
isAnimation = scope.animation || false
$window = angular.element(window),
doc = document.documentElement,
startPosition,
currPosition,
setSticky,
reset;
if (isAnimation) {
element.css('-webkit-transition', 'height 0s ease-out, background-color 1s ease-out');
element.css('-moz-transition', 'height 0s ease-out, background-color 1s ease-out');
element.css('-ms-transition', 'height 0s ease-out, background-color 1s ease-out');
element.css('-o-transition', 'height 0s ease-out, background-color 1s ease-out');
element.css('transition', 'height 0s ease-out, background-color 1s ease-out');
}
// Set appropriate style based on the location
switch (locate) {
case 'left':
currPosition = function() {
return (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0)
};
startPosition = scope.at * window.innerWidth || element.offset().left+1;
setSticky = function() {
element.addClass('sticky-left');
};
reset = function () {
element.removeClass('sticky-left');
};
break;
case 'bottom':
currPosition = function() {
return (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
};
startPosition = scope.at * window.innerHeight || 0;
setSticky = function() {
element.addClass('sticky-bottom');
};
reset = function () {
element.removeClass('sticky-bottom');
};
break;
case 'right':
currPosition = function() {
return (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0)
};
startPosition = scope.at * window.innerWidth || 0;
setSticky = function() {
element.addClass('sticky-right');
};
reset = function () {
element.removeClass('sticky-right');
};
break;
default :
currPosition = function() {
return (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
};
startPosition = scope.at * window.innerHeight || element.offset().top+1;
setSticky = function() {
element.addClass('sticky-top');
};
reset = function () {
element.removeClass('sticky-top');
};
break;
}
// Check if the $window has passed the start line, if it has passed, set element to sticky style
function check() {
if (currPosition() >= startPosition) {
setSticky();
}
else
{
reset();
}
}
// Handle the resize event
function resize() {
switch (locate) {
case 'left':
startPosition = scope.at * window.innerHeight || element.offset().left+1;
break;
case 'bottom':
startPosition = scope.at * window.innerHeight || 0;
break;
case 'right':
startPosition = scope.at * window.innerHeight || 0;
break;
default :
startPosition = scope.at * window.innerHeight || element.offset().top+1;
break;
}
}
// Attach our listeners
$window.on('scroll', check);
$window.on('resize', resize);
check();
}
};
}]);