Skip to content
This repository was archived by the owner on Jan 11, 2022. It is now read-only.

Commit 64f27d0

Browse files
author
rgalus
committed
Merge branch 'develop'
2 parents 5e3a635 + 13e9a4c commit 64f27d0

File tree

5 files changed

+153
-148
lines changed

5 files changed

+153
-148
lines changed

.npmignore

+5
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1+
.DS_Store
12
/demo
3+
/node_modules
4+
npm-debug.log
5+
.editorconfig
6+
gulpfile.babel.js

dist/sticky.compile.js

+77-72
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
1313

1414
var Sticky = function () {
1515
/**
16-
* Sticky instance constructor.
16+
* Sticky instance constructor
1717
* @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)
2020
*/
2121
function Sticky() {
2222
var selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
@@ -42,7 +42,7 @@ var Sticky = function () {
4242
}
4343

4444
/**
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
4646
* @function
4747
*/
4848

@@ -64,16 +64,16 @@ var Sticky = function () {
6464
};
6565

6666
/**
67-
* Renders sticky element
67+
* Function that assign needed variables for sticky element, that are used in future for calculations and other
6868
* @function
69-
* @param {node} element - Sticky element to be rendered.
69+
* @param {node} element - Element to be rendered
7070
*/
7171

7272

7373
Sticky.prototype.renderElement = function renderElement(element) {
7474
var _this2 = this;
7575

76-
// create container where are needed variables are placed
76+
// create container for variables needed in future
7777
element.sticky = {};
7878

7979
// set default variables
@@ -88,7 +88,7 @@ var Sticky = function () {
8888

8989
element.sticky.rect = this.getRectangle(element);
9090

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
9292
if (element.tagName.toLowerCase === 'img') {
9393
element.onload = function () {
9494
return element.sticky.rect = _this2.getRectangle(element);
@@ -100,46 +100,46 @@ var Sticky = function () {
100100
};
101101

102102
/**
103-
* Activate element if breakpoint
103+
* Function that activates element when specified conditions are met and then initalise events
104104
* @function
105-
* @param {node} element - Sticky element to be activated.
105+
* @param {node} element - Element to be activated
106106
*/
107107

108108

109109
Sticky.prototype.activate = function activate(element) {
110-
var heightBefore = void 0,
111-
heightAfter = void 0;
110+
var heightBefore = element.sticky.container.offsetHeight;
112111

113-
heightBefore = element.sticky.container.offsetHeight;
112+
this.css(element, { position: 'fixed' });
114113

115-
this.addStyle(element, { position: 'fixed' });
114+
var heightAfter = element.sticky.container.offsetHeight;
116115

117-
heightAfter = element.sticky.container.offsetHeight;
118-
119-
this.removeStyle(element, ['position']);
116+
this.css(element, { position: '' });
120117

121118
if (heightAfter >= heightBefore && element.sticky.stickyFor < this.vp.width && !element.sticky.active) {
122119
element.sticky.active = true;
123120
}
124121

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+
}
129125

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+
}
133130

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+
};
137136

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+
*/
139142

140-
this.initResizeEvents(element);
141-
this.initScrollEvents(element);
142-
};
143143

144144
Sticky.prototype.initResizeEvents = function initResizeEvents(element) {
145145
var _this3 = this;
@@ -150,6 +150,13 @@ var Sticky = function () {
150150
window.addEventListener('resize', element.sticky.resizeListener);
151151
};
152152

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+
153160
Sticky.prototype.onResizeEvents = function onResizeEvents(element) {
154161
this.vp = this.getViewportSize();
155162

@@ -165,6 +172,13 @@ var Sticky = function () {
165172
this.setPosition(element);
166173
};
167174

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+
168182
Sticky.prototype.initScrollEvents = function initScrollEvents(element) {
169183
var _this4 = this;
170184

@@ -174,6 +188,13 @@ var Sticky = function () {
174188
window.addEventListener('scroll', element.sticky.scrollListener);
175189
};
176190

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+
177198
Sticky.prototype.onScrollEvents = function onScrollEvents(element) {
178199
this.scrollTop = this.getScrollTopPosition();
179200

@@ -183,15 +204,14 @@ var Sticky = function () {
183204
};
184205

185206
/**
186-
* Function
207+
* Main function for the library. Here are some condition calculations and css appending for sticky element when user scroll window
187208
* @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
190210
*/
191211

192212

193213
Sticky.prototype.setPosition = function setPosition(element) {
194-
this.removeStyle(element, ['position', 'width', 'top', 'left']);
214+
this.css(element, { position: '', width: '', top: '', left: '' });
195215

196216
if (this.vp.height < element.sticky.rect.height || !element.sticky.active) {
197217
return;
@@ -202,7 +222,7 @@ var Sticky = function () {
202222
}
203223

204224
if (this.scrollTop > element.sticky.rect.top - element.sticky.marginTop) {
205-
this.addStyle(element, {
225+
this.css(element, {
206226
position: 'fixed',
207227
width: element.sticky.rect.width + 'px',
208228
left: element.sticky.rect.left + 'px'
@@ -212,22 +232,22 @@ var Sticky = function () {
212232

213233
if (element.sticky.stickyClass) element.classList.remove(element.sticky.stickyClass);
214234

215-
this.addStyle(element, {
235+
this.css(element, {
216236
top: element.sticky.container.rect.top + element.sticky.container.offsetHeight - (this.scrollTop + element.sticky.rect.height) + 'px' });
217237
} else {
218238
if (element.sticky.stickyClass) element.classList.add(element.sticky.stickyClass);
219239

220-
this.addStyle(element, { top: element.sticky.marginTop + 'px' });
240+
this.css(element, { top: element.sticky.marginTop + 'px' });
221241
}
222242
} else {
223243
if (element.sticky.stickyClass) element.classList.remove(element.sticky.stickyClass);
224244

225-
this.removeStyle(element, ['position', 'width', 'top', 'left']);
245+
this.css(element, { position: '', width: '', top: '', left: '' });
226246
}
227247
};
228248

229249
/**
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
231251
* @function
232252
*/
233253

@@ -239,14 +259,15 @@ var Sticky = function () {
239259
element.sticky.rect = _this5.getRectangle(element);
240260
element.sticky.container.rect = _this5.getRectangle(element.sticky.container);
241261

262+
_this5.activate(element);
242263
_this5.setPosition(element);
243264
});
244265
};
245266

246267
/**
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)
248269
* @function
249-
* @param {node} element - Element which sticky container are looked for.
270+
* @param {node} element - Element which sticky container are looked for
250271
* @return {node} element - Sticky container
251272
*/
252273

@@ -262,23 +283,21 @@ var Sticky = function () {
262283
};
263284

264285
/**
265-
* Function that returns element rectangle & position (width, height, top, left).
286+
* Function that returns element rectangle & position (width, height, top, left)
266287
* @function
267-
* @param {node} element - Element which position & rectangle are calculated.
288+
* @param {node} element - Element which position & rectangle are returned
268289
* @return {object}
269290
*/
270291

271292

272293
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);
274298

275299
var top = 0;
276300
var left = 0;
277-
var width = 0;
278-
var height = 0;
279-
280-
width = element.offsetWidth;
281-
height = element.offsetHeight;
282301

283302
do {
284303
top += element.offsetTop || 0;
@@ -290,7 +309,7 @@ var Sticky = function () {
290309
};
291310

292311
/**
293-
* Function that returns viewport dimensions in object.
312+
* Function that returns viewport dimensions
294313
* @function
295314
* @return {object}
296315
*/
@@ -304,7 +323,7 @@ var Sticky = function () {
304323
};
305324

306325
/**
307-
* Function that returns scroll top position in pixels.
326+
* Function that returns scroll position offset from top
308327
* @function
309328
* @return {number}
310329
*/
@@ -315,10 +334,10 @@ var Sticky = function () {
315334
};
316335

317336
/**
318-
* Helper function for loops.
337+
* Helper function for loops
319338
* @helper
320339
* @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)
322341
*/
323342

324343

@@ -329,35 +348,21 @@ var Sticky = function () {
329348
};
330349

331350
/**
332-
* Helper function to easy add css properties to specified element.
351+
* Helper function to add/remove css properties for specified element.
333352
* @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
336355
*/
337356

338357

339-
Sticky.prototype.addStyle = function addStyle(element, properties) {
358+
Sticky.prototype.css = function css(element, properties) {
340359
for (var property in properties) {
341360
if (properties.hasOwnProperty(property)) {
342361
element.style[property] = properties[property];
343362
}
344363
}
345364
};
346365

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-
361366
return Sticky;
362367
}();
363368

0 commit comments

Comments
 (0)