-
Notifications
You must be signed in to change notification settings - Fork 24
/
sticky.js
88 lines (77 loc) · 2.55 KB
/
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
angular.module('sticky', []);
function toPx(n){
if(!isNaN(n)){
return n+"px";
} else {
return n;
}
}
function windowScrollTop() {
var body = document.documentElement || document.body;
return window.pageYOffset || body.scrollTop;
}
function getStyle(el,styleProp){
if (el.currentStyle)
var y = el.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
return y;
}
angular.module('sticky', []).directive("sticky", function($window) {
return {
link: function(scope, element, attrs) {
var $win = angular.element($window);
var stickyItem = function(item){
item.element.attr("style","position:fixed;top:"+toPx(item.topOffset)+";left:"+(item.left-item.leftOffset)+"px;width:"+item.width+"px;");
item.element.addClass("stuck");
item.isStuck = true;
};
var unStickyItem = function(item){
item.element.removeClass("stuck");
item.element.attr("style","");
item.isStuck = false;
};
if (scope._stickyElements === undefined) {
scope._stickyElements = [];
$win.bind("scroll", function(e) {
var pos = windowScrollTop();
for (var i=0; i<scope._stickyElements.length; i++) {
var item = scope._stickyElements[i];
if (!item.isStuck && pos > item.start) {
stickyItem(item);
}
else if (item.isStuck && pos < item.start) {
unStickyItem(item);
}
}
});
var updateItem = function(item){
item.topOffset = scope.$apply(attrs.stickyTop);
item.leftOffset = scope.$apply(attrs.stickyLeft);
item.start = item.element[0].offsetTop-item.topOffset-parseInt(getStyle(item.element[0],'margin-top'),10);
item.width = item.element[0].offsetWidth;
item.left = item.element[0].getBoundingClientRect().left;
}
var recheckPositions = function() {
for (var i=0; i<scope._stickyElements.length; i++) {
var item = scope._stickyElements[i];
if (!item.isStuck) {
updateItem(item);
} else {
unStickyItem(item);
updateItem(item);
stickyItem(item);
}
}
};
$win.bind("load", recheckPositions);
$win.bind("resize", recheckPositions);
}
var item = {
element: element,
isStuck: false
};
scope._stickyElements.push(item);
}
};
});