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

Commit 711abaa

Browse files
author
rgalus
committed
Merge branch 'develop'
2 parents 298d1af + fe90674 commit 711abaa

File tree

5 files changed

+54
-19
lines changed

5 files changed

+54
-19
lines changed

dist/sticky.compile.js

+27-11
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ var _createClass = function () { function defineProperties(target, props) { for
55
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
66

77
var Sticky = function () {
8-
function Sticky(element) {
8+
function Sticky(selector) {
99
_classCallCheck(this, Sticky);
1010

11-
this.element = element;
11+
this.selector = selector;
1212

1313
this.vp = this.getViewportSize();
1414
this.scrollTop = this.getScrollTopPosition();
@@ -21,17 +21,18 @@ var Sticky = function () {
2121
value: function initialize() {
2222
var _this = this;
2323

24-
var stickyElements = document.querySelectorAll(this.element);
24+
this.elements = document.querySelectorAll(this.selector);
2525

26-
var _loop = function _loop(i, len) {
27-
window.addEventListener('load', function () {
28-
return _this.activate(stickyElements[i]);
29-
});
30-
};
26+
// initialize sticky only when dom is fully loaded
27+
var DOMContentLoaded = setInterval(function () {
28+
if (document.readyState === 'interactive' || document.readyState === 'complete') {
29+
for (var i = 0, len = _this.elements.length; i < len; i++) {
30+
_this.activate(_this.elements[i]);
31+
}
3132

32-
for (var i = 0, len = stickyElements.length; i < len; i++) {
33-
_loop(i, len);
34-
}
33+
clearInterval(DOMContentLoaded);
34+
}
35+
}, 100);
3536
}
3637
}, {
3738
key: 'activate',
@@ -43,6 +44,13 @@ var Sticky = function () {
4344
el.sticky.marginTop = el.getAttribute('data-margin-top') ? parseInt(el.getAttribute('data-margin-top')) : 0;
4445
el.sticky.rect = this.getRect(el);
4546

47+
// fix when el is image that has not yet loaded and width, height = 0
48+
if (el.tagName.toLowerCase() === 'img') {
49+
el.onload = function () {
50+
return el.sticky.rect = _this2.getRect(el);
51+
};
52+
}
53+
4654
el.sticky.container = this.getContainer(el);
4755
el.sticky.container.rect = this.getRect(el.sticky.container);
4856

@@ -145,6 +153,14 @@ var Sticky = function () {
145153
this.removeStyle(el, ['position', 'width', 'top', 'left', 'right', 'bottom']);
146154
}
147155
}
156+
}, {
157+
key: 'update',
158+
value: function update() {
159+
for (var i = 0, len = this.elements.length; i < len; i++) {
160+
this.updateRect(this.elements[i]);
161+
this.setPosition(this.elements[i]);
162+
}
163+
}
148164
}, {
149165
key: 'addStyle',
150166
value: function addStyle(el, styles) {

dist/sticky.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/sticky.min.js.gz

106 Bytes
Binary file not shown.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sticky-js",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Sticky elements",
55
"main": "dist/sticky.min.js",
66
"scripts": {

src/sticky.js

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
class Sticky {
3-
constructor(element) {
4-
this.element = element;
3+
constructor(selector) {
4+
this.selector = selector;
55

66
this.vp = this.getViewportSize();
77
this.scrollTop = this.getScrollTopPosition();
@@ -10,11 +10,18 @@ class Sticky {
1010
}
1111

1212
initialize() {
13-
const stickyElements = document.querySelectorAll(this.element);
13+
this.elements = document.querySelectorAll(this.selector);
1414

15-
for (let i = 0, len = stickyElements.length; i < len; i++) {
16-
window.addEventListener('load', () => this.activate(stickyElements[i]));
17-
}
15+
// initialize sticky only when dom is fully loaded
16+
const DOMContentLoaded = setInterval(() => {
17+
if (document.readyState === 'interactive' || document.readyState === 'complete') {
18+
for (let i = 0, len = this.elements.length; i < len; i++) {
19+
this.activate(this.elements[i]);
20+
}
21+
22+
clearInterval(DOMContentLoaded);
23+
}
24+
}, 100);
1825
}
1926

2027
activate(el) {
@@ -23,6 +30,11 @@ class Sticky {
2330
el.sticky.marginTop = el.getAttribute('data-margin-top') ? parseInt(el.getAttribute('data-margin-top')) : 0;
2431
el.sticky.rect = this.getRect(el);
2532

33+
// fix when el is image that has not yet loaded and width, height = 0
34+
if (el.tagName.toLowerCase() === 'img') {
35+
el.onload = () => el.sticky.rect = this.getRect(el);
36+
}
37+
2638
el.sticky.container = this.getContainer(el);
2739
el.sticky.container.rect = this.getRect(el.sticky.container);
2840

@@ -117,6 +129,13 @@ class Sticky {
117129
}
118130
}
119131

132+
update() {
133+
for (let i = 0, len = this.elements.length; i < len; i++) {
134+
this.updateRect(this.elements[i]);
135+
this.setPosition(this.elements[i]);
136+
}
137+
}
138+
120139
addStyle(el, styles) {
121140
for (let property in styles) {
122141
if (styles.hasOwnProperty(property)) {

0 commit comments

Comments
 (0)