-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsticky.js
212 lines (174 loc) · 5.59 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* Sticky position that works everywhere
*
* MIT licensed. By Afshin Mehrabani <[email protected]>
*
* This project is a part of Kissui framework.
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['kissuiPosition'], function (kissuiPosition) {
return (root.kissuiSticky = factory(kissuiPosition));
});
} else {
root.kissuiSticky = factory(root.kissuiPosition);
}
}(this, function (kissuiPosition) {
/**
* options
*/
var _options = {
attribute: 'data-kui-sticky',
placeholderId: 'kissui-sticky-placeholder-'
};
/**
* To store all available elements with their options
*/
var _elements = [];
/**
* Find elements
*/
function _populate () {
//clear old elements first
_elements = [];
var elements = document.querySelectorAll('*[' + _options.attribute + ']');
for (var i = 0;i < elements.length;i++) {
var element = elements[i];
var className = element.getAttribute(_options.attribute);
var opts = {};
if (className && className != '') {
opts['className'] = className;
}
// to restore the cssText later
opts['cssText'] = element.style.cssText;
_add(element, opts);
}
};
/**
* Adds a new item to _elements array
*/
function _add (element, opts) {
_elements.push({
element: element,
active: false,
opts: opts
});
kissuiPosition.add(element, 'partially out top');
};
/**
* Finds an element by looking into the _elements
*
*/
function _find (element) {
for (var i = 0;i < _elements.length; i++) {
var elx = _elements[i];
if (element === elx.element) {
return {
element: elx,
i: i
};
}
}
return null;
};
/**
* Restore the classes and removing the placeholder of top event
*
*/
function _restoreTop (id, elx) {
if (elx.active) {
var element = elx.element;
elx.active = false;
// placeholder
var placeholder = document.getElementById(_options.placeholderId + id);
placeholder.parentElement.removeChild(placeholder);
element.className = element.className.replace('kui sticky element', '').trim();
if (/scrolled/gi.test(element.className)) {
element.className = element.className.replace('scrolled', '').trim();
}
if (typeof (elx.opts.className) != 'undefined') {
element.className = element.className.replace(elx.opts.className, '').trim();
}
element.style.cssText = elx.opts['cssText'];
}
};
/**
* To handle top event
*
*/
function _handleTop (element, event) {
// is this a pladeholder?
if (element.id.indexOf(_options.placeholderId) > -1) {
var id = parseInt(element.getAttribute('data-id'), 10);
_restoreTop(id, _elements[id]);
} else {
var elxObj = _find(element);
var elx = elxObj.element;
if (!elx.active) {
var props = element.getBoundingClientRect();
var computedStyle = element.currentStyle || window.getComputedStyle(element);
// adding the placeholder instead of the `fixed` position element
var placeholder = document.createElement('div');
placeholder.className = 'kui sticky placeholder'
// width/height
placeholder.style.width = props.width + 'px';
placeholder.style.height = props.height + 'px';
// margins
placeholder.style.marginTop = computedStyle.marginTop;
placeholder.style.marginBottom = computedStyle.marginBottom;
placeholder.style.marginLeft = computedStyle.marginLeft;
placeholder.style.marginRight = computedStyle.marginRight;
// id
placeholder.id = 'kissui-sticky-placeholder-' + elxObj.i;
placeholder.setAttribute('data-id', elxObj.i);
element.parentElement.insertBefore(placeholder, element);
// adding placeholder to kissuiPosition to be able to restore the element later
kissuiPosition.add(placeholder, 'in');
kissuiPosition.add(placeholder, 'top');
element.className += ' kui sticky element';
if (typeof (elx.opts.className) != 'undefined') {
element.className += ' ' + elx.opts.className;
}
element.style.cssText += 'left: ' + props.left + 'px!important;';
element.style.cssText += 'width:' + props.width + 'px!important;';
var elementHeight = parseInt(computedStyle.marginTop) + props.height;
if (elementHeight > window.innerHeight) {
element.style.cssText += 'height: 100%!important;';
element.className += ' scrolled';
} else {
element.style.cssText += 'height:' + props.height + 'px!important;';
}
// set element's active flag to true so we can deactivate the sticky position later
elx.active = true;
}
}
};
/**
* Start the module
*/
function _init () {
_populate.call(this);
kissuiPosition.on('top', function (element, event) {
_handleTop(element, event);
});
kissuiPosition.on('in', function (element, event) {
_handleTop(element, event);
});
kissuiPosition.on('partially out top', function (element, event) {
// it means when the element is completely out of viewport or even partially
// so we try to call the _handle* method
if (element.id.indexOf(_options.placeholderId) == -1) {
// we call this only for non-placeholder elements
_handleTop(element, event);
}
});
kissuiPosition.init();
};
_init();
return {
_options: _options,
_elements: _elements,
init: _init,
add: _add
};
}));