@@ -13,10 +13,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
13
13
14
14
var Sticky = function ( ) {
15
15
/**
16
- * Sticky instance constructor.
16
+ * Sticky instance constructor
17
17
* @constructor
18
- * @param {string } selector - Selector which we can find elements.
19
- * @param {string } options - Global options for sticky elements (could be overwritten by data-{option}="" attributes).
18
+ * @param {string } selector - Selector which we can find elements
19
+ * @param {string } options - Global options for sticky elements (could be overwritten by data-{option}="" attributes)
20
20
*/
21
21
function Sticky ( ) {
22
22
var selector = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : '' ;
@@ -42,7 +42,7 @@ var Sticky = function () {
42
42
}
43
43
44
44
/**
45
- * Activate Sticky library
45
+ * Function that waits for page to be fully loaded and then renders & activates every sticky element found with specified selector
46
46
* @function
47
47
*/
48
48
@@ -64,16 +64,16 @@ var Sticky = function () {
64
64
} ;
65
65
66
66
/**
67
- * Renders sticky element
67
+ * Function that assign needed variables for sticky element, that are used in future for calculations and other
68
68
* @function
69
- * @param {node } element - Sticky element to be rendered.
69
+ * @param {node } element - Element to be rendered
70
70
*/
71
71
72
72
73
73
Sticky . prototype . renderElement = function renderElement ( element ) {
74
74
var _this2 = this ;
75
75
76
- // create container where are needed variables are placed
76
+ // create container for variables needed in future
77
77
element . sticky = { } ;
78
78
79
79
// set default variables
@@ -88,7 +88,7 @@ var Sticky = function () {
88
88
89
89
element . sticky . rect = this . getRectangle ( element ) ;
90
90
91
- // fix when el is image that has not yet loaded and width, height = 0
91
+ // fix when element is image that has not yet loaded and width, height = 0
92
92
if ( element . tagName . toLowerCase === 'img' ) {
93
93
element . onload = function ( ) {
94
94
return element . sticky . rect = _this2 . getRectangle ( element ) ;
@@ -100,46 +100,46 @@ var Sticky = function () {
100
100
} ;
101
101
102
102
/**
103
- * Activate element if breakpoint
103
+ * Function that activates element when specified conditions are met and then initalise events
104
104
* @function
105
- * @param {node } element - Sticky element to be activated.
105
+ * @param {node } element - Element to be activated
106
106
*/
107
107
108
108
109
109
Sticky . prototype . activate = function activate ( element ) {
110
- var heightBefore = void 0 ,
111
- heightAfter = void 0 ;
110
+ var heightBefore = element . sticky . container . offsetHeight ;
112
111
113
- heightBefore = element . sticky . container . offsetHeight ;
112
+ this . css ( element , { position : 'fixed' } ) ;
114
113
115
- this . addStyle ( element , { position : 'fixed' } ) ;
114
+ var heightAfter = element . sticky . container . offsetHeight ;
116
115
117
- heightAfter = element . sticky . container . offsetHeight ;
118
-
119
- this . removeStyle ( element , [ 'position' ] ) ;
116
+ this . css ( element , { position : '' } ) ;
120
117
121
118
if ( heightAfter >= heightBefore && element . sticky . stickyFor < this . vp . width && ! element . sticky . active ) {
122
119
element . sticky . active = true ;
123
120
}
124
121
125
- // this.addStyle(element, {
126
- // '-webkit-transform': 'translate3d(0, 0, 0)',
127
- // '-ms-transform': 'translate3d(0, 0, 0)',
128
- // 'transform': 'translate3d(0, 0, 0)',
122
+ if ( this . elements . indexOf ( element ) < 0 ) {
123
+ this . elements . push ( element ) ;
124
+ }
129
125
130
- // '-webkit-perspective': 1000,
131
- // '-ms-perspective': 1000,
132
- // 'perspective': 1000,
126
+ if ( ! element . sticky . resizeEvent ) {
127
+ this . initResizeEvents ( element ) ;
128
+ element . sticky . resizeEvent = true ;
129
+ }
133
130
134
- // '-webkit-backface-visibility': 'hidden',
135
- // 'backface-visibility': 'hidden',
136
- // });
131
+ if ( ! element . sticky . scrollEvent ) {
132
+ this . initScrollEvents ( element ) ;
133
+ element . sticky . scrollEvent = true ;
134
+ }
135
+ } ;
137
136
138
- this . elements . push ( element ) ;
137
+ /**
138
+ * Function which is adding onResizeEvents to window listener and assigns function to element as resizeListener
139
+ * @function
140
+ * @param {node } element - Element for which resize events are initialised
141
+ */
139
142
140
- this . initResizeEvents ( element ) ;
141
- this . initScrollEvents ( element ) ;
142
- } ;
143
143
144
144
Sticky . prototype . initResizeEvents = function initResizeEvents ( element ) {
145
145
var _this3 = this ;
@@ -150,6 +150,13 @@ var Sticky = function () {
150
150
window . addEventListener ( 'resize' , element . sticky . resizeListener ) ;
151
151
} ;
152
152
153
+ /**
154
+ * Function which is fired when user resize window. It checks if element should be activated or deactivated and then run setPosition function
155
+ * @function
156
+ * @param {node } element - Element for which event function is fired
157
+ */
158
+
159
+
153
160
Sticky . prototype . onResizeEvents = function onResizeEvents ( element ) {
154
161
this . vp = this . getViewportSize ( ) ;
155
162
@@ -165,6 +172,13 @@ var Sticky = function () {
165
172
this . setPosition ( element ) ;
166
173
} ;
167
174
175
+ /**
176
+ * Function which is adding onScrollEvents to window listener and assigns function to element as scrollListener
177
+ * @function
178
+ * @param {node } element - Element for which scroll events are initialised
179
+ */
180
+
181
+
168
182
Sticky . prototype . initScrollEvents = function initScrollEvents ( element ) {
169
183
var _this4 = this ;
170
184
@@ -174,6 +188,13 @@ var Sticky = function () {
174
188
window . addEventListener ( 'scroll' , element . sticky . scrollListener ) ;
175
189
} ;
176
190
191
+ /**
192
+ * Function which is fired when user scroll window. If element is active, function is invoking setPosition function
193
+ * @function
194
+ * @param {node } element - Element for which event function is fired
195
+ */
196
+
197
+
177
198
Sticky . prototype . onScrollEvents = function onScrollEvents ( element ) {
178
199
this . scrollTop = this . getScrollTopPosition ( ) ;
179
200
@@ -183,15 +204,14 @@ var Sticky = function () {
183
204
} ;
184
205
185
206
/**
186
- * Function
207
+ * Main function for the library. Here are some condition calculations and css appending for sticky element when user scroll window
187
208
* @function
188
- * @param {node } element - Element which sticky container are looked for.
189
- * @return {node } element - Sticky container
209
+ * @param {node } element - Element that will be positioned if it's active
190
210
*/
191
211
192
212
193
213
Sticky . prototype . setPosition = function setPosition ( element ) {
194
- this . removeStyle ( element , [ ' position' , ' width' , ' top' , ' left' ] ) ;
214
+ this . css ( element , { position : '' , width : '' , top : '' , left : '' } ) ;
195
215
196
216
if ( this . vp . height < element . sticky . rect . height || ! element . sticky . active ) {
197
217
return ;
@@ -202,7 +222,7 @@ var Sticky = function () {
202
222
}
203
223
204
224
if ( this . scrollTop > element . sticky . rect . top - element . sticky . marginTop ) {
205
- this . addStyle ( element , {
225
+ this . css ( element , {
206
226
position : 'fixed' ,
207
227
width : element . sticky . rect . width + 'px' ,
208
228
left : element . sticky . rect . left + 'px'
@@ -212,22 +232,22 @@ var Sticky = function () {
212
232
213
233
if ( element . sticky . stickyClass ) element . classList . remove ( element . sticky . stickyClass ) ;
214
234
215
- this . addStyle ( element , {
235
+ this . css ( element , {
216
236
top : element . sticky . container . rect . top + element . sticky . container . offsetHeight - ( this . scrollTop + element . sticky . rect . height ) + 'px' } ) ;
217
237
} else {
218
238
if ( element . sticky . stickyClass ) element . classList . add ( element . sticky . stickyClass ) ;
219
239
220
- this . addStyle ( element , { top : element . sticky . marginTop + 'px' } ) ;
240
+ this . css ( element , { top : element . sticky . marginTop + 'px' } ) ;
221
241
}
222
242
} else {
223
243
if ( element . sticky . stickyClass ) element . classList . remove ( element . sticky . stickyClass ) ;
224
244
225
- this . removeStyle ( element , [ ' position' , ' width' , ' top' , ' left' ] ) ;
245
+ this . css ( element , { position : '' , width : '' , top : '' , left : '' } ) ;
226
246
}
227
247
} ;
228
248
229
249
/**
230
- * Function that updates element sticky rectangle (with parent container) and then update position;
250
+ * Function that updates element sticky rectangle (with sticky container), then activate or deactivate element, then update position if it's active
231
251
* @function
232
252
*/
233
253
@@ -239,14 +259,15 @@ var Sticky = function () {
239
259
element . sticky . rect = _this5 . getRectangle ( element ) ;
240
260
element . sticky . container . rect = _this5 . getRectangle ( element . sticky . container ) ;
241
261
262
+ _this5 . activate ( element ) ;
242
263
_this5 . setPosition ( element ) ;
243
264
} ) ;
244
265
} ;
245
266
246
267
/**
247
- * Function that returns element in which sticky is stuck (if is not specified, then it is sticky document body);
268
+ * Function that returns container element in which sticky element is stuck (if is not specified, then it's stuck to body)
248
269
* @function
249
- * @param {node } element - Element which sticky container are looked for.
270
+ * @param {node } element - Element which sticky container are looked for
250
271
* @return {node } element - Sticky container
251
272
*/
252
273
@@ -262,23 +283,21 @@ var Sticky = function () {
262
283
} ;
263
284
264
285
/**
265
- * Function that returns element rectangle & position (width, height, top, left).
286
+ * Function that returns element rectangle & position (width, height, top, left)
266
287
* @function
267
- * @param {node } element - Element which position & rectangle are calculated.
288
+ * @param {node } element - Element which position & rectangle are returned
268
289
* @return {object }
269
290
*/
270
291
271
292
272
293
Sticky . prototype . getRectangle = function getRectangle ( element ) {
273
- this . removeStyle ( element , [ 'position' , 'width' , 'top' , 'left' ] ) ;
294
+ this . css ( element , { position : '' , width : '' , top : '' , left : '' } ) ;
295
+
296
+ var width = Math . max ( element . offsetWidth , element . clientWidth , element . scrollWidth ) ;
297
+ var height = Math . max ( element . offsetHeight , element . clientHeight , element . scrollHeight ) ;
274
298
275
299
var top = 0 ;
276
300
var left = 0 ;
277
- var width = 0 ;
278
- var height = 0 ;
279
-
280
- width = element . offsetWidth ;
281
- height = element . offsetHeight ;
282
301
283
302
do {
284
303
top += element . offsetTop || 0 ;
@@ -290,7 +309,7 @@ var Sticky = function () {
290
309
} ;
291
310
292
311
/**
293
- * Function that returns viewport dimensions in object.
312
+ * Function that returns viewport dimensions
294
313
* @function
295
314
* @return {object }
296
315
*/
@@ -304,7 +323,7 @@ var Sticky = function () {
304
323
} ;
305
324
306
325
/**
307
- * Function that returns scroll top position in pixels.
326
+ * Function that returns scroll position offset from top
308
327
* @function
309
328
* @return {number }
310
329
*/
@@ -315,10 +334,10 @@ var Sticky = function () {
315
334
} ;
316
335
317
336
/**
318
- * Helper function for loops.
337
+ * Helper function for loops
319
338
* @helper
320
339
* @param {array }
321
- * @param {function } callback - Callback function thaht will be invoked to every element from array.
340
+ * @param {function } callback - Callback function (no need for explanation)
322
341
*/
323
342
324
343
@@ -329,35 +348,21 @@ var Sticky = function () {
329
348
} ;
330
349
331
350
/**
332
- * Helper function to easy add css properties to specified element.
351
+ * Helper function to add/remove css properties for specified element.
333
352
* @helper
334
- * @param {node } element - DOM element.
335
- * @param {object } properties - CSS properties that will be added to the specified element.
353
+ * @param {node } element - DOM element
354
+ * @param {object } properties - CSS properties that will be added/removed from specified element
336
355
*/
337
356
338
357
339
- Sticky . prototype . addStyle = function addStyle ( element , properties ) {
358
+ Sticky . prototype . css = function css ( element , properties ) {
340
359
for ( var property in properties ) {
341
360
if ( properties . hasOwnProperty ( property ) ) {
342
361
element . style [ property ] = properties [ property ] ;
343
362
}
344
363
}
345
364
} ;
346
365
347
- /**
348
- * Helper function to easy remove css properties from specified element.
349
- * @helper
350
- * @param {node } element - DOM element.
351
- * @param {object } properties - CSS properties that will be removed from the specified element.
352
- */
353
-
354
-
355
- Sticky . prototype . removeStyle = function removeStyle ( element , properties ) {
356
- this . forEach ( properties , function ( property ) {
357
- element . style [ property ] = '' ;
358
- } ) ;
359
- } ;
360
-
361
366
return Sticky ;
362
367
} ( ) ;
363
368
0 commit comments