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

Commit 540f33b

Browse files
author
rgalus
committed
Merge branch 'develop'
2 parents 3571251 + 4b340ea commit 540f33b

8 files changed

+44
-36
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Sticky-js is a library for sticky elements written in vanilla javascript. With t
99
## Features
1010

1111
- Written in vanilla javascript, no dependencies needed
12-
- Lightweight (minified: ~4.75kb, gzipped: ~1.41kb)
12+
- Lightweight (minified: ~5.12kb, gzipped: ~1.45kb)
1313
- It can be sticky to the entire page or to selected parent container
1414
- No additional CSS needed
1515

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
"test",
1919
"tests"
2020
],
21-
"version": "1.1.4"
21+
"version": "1.1.5"
2222
}

demo/index.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
}
4141

4242
.top-bar {
43-
position: fixed;
44-
top: 0;
45-
left: 0;
43+
/*position: fixed;*/
44+
/*top: 0;*/
45+
/*left: 0;*/
4646

47-
width: 100%;
47+
/*width: 100%;*/
4848
}
4949

5050
.top-bar,

dist/sticky.compile.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
44
* Sticky.js
55
* Library for sticky elements written in vanilla javascript. With this library you can easily set sticky elements on your website. It's also responsive.
66
*
7-
* @version 1.1.4
7+
* @version 1.1.5
88
* @author Rafal Galus <[email protected]>
99
* @website https://rgalus.github.io/sticky-js/
1010
* @repo https://github.com/rgalus/sticky-js
@@ -27,7 +27,7 @@ var Sticky = function () {
2727
this.selector = selector;
2828
this.elements = [];
2929

30-
this.version = '1.1.4';
30+
this.version = '1.1.5';
3131

3232
this.vp = this.getViewportSize();
3333
this.scrollTop = this.getScrollTopPosition();
@@ -107,15 +107,7 @@ var Sticky = function () {
107107

108108

109109
Sticky.prototype.activate = function activate(element) {
110-
var heightBefore = element.sticky.container.offsetHeight;
111-
112-
this.css(element, { position: 'fixed' });
113-
114-
var heightAfter = element.sticky.container.offsetHeight;
115-
116-
this.css(element, { position: '' });
117-
118-
if (heightAfter >= heightBefore && element.sticky.stickyFor < this.vp.width && !element.sticky.active) {
110+
if (element.sticky.rect.top + element.sticky.rect.height < element.sticky.container.rect.top + element.sticky.container.rect.height && element.sticky.stickyFor < this.vp.width && !element.sticky.active) {
119111
element.sticky.active = true;
120112
}
121113

@@ -132,6 +124,8 @@ var Sticky = function () {
132124
this.initScrollEvents(element);
133125
element.sticky.scrollEvent = true;
134126
}
127+
128+
this.setPosition(element);
135129
};
136130

137131
/**
@@ -163,9 +157,9 @@ var Sticky = function () {
163157
element.sticky.rect = this.getRectangle(element);
164158
element.sticky.container.rect = this.getRectangle(element.sticky.container);
165159

166-
if (element.sticky.stickyFor < this.vp.width && !element.sticky.active) {
160+
if (element.sticky.rect.top + element.sticky.rect.height < element.sticky.container.rect.top + element.sticky.container.rect.height && element.sticky.stickyFor < this.vp.width && !element.sticky.active) {
167161
element.sticky.active = true;
168-
} else if (element.sticky.stickyFor >= this.vp.width && element.sticky.active) {
162+
} else if (element.sticky.rect.top + element.sticky.rect.height >= element.sticky.container.rect.top + element.sticky.container.rect.height || element.sticky.stickyFor >= this.vp.width && element.sticky.active) {
169163
element.sticky.active = false;
170164
}
171165

@@ -221,7 +215,14 @@ var Sticky = function () {
221215
element.sticky.rect = this.getRectangle(element);
222216
}
223217

224-
if (this.scrollTop > element.sticky.rect.top - element.sticky.marginTop) {
218+
if (element.sticky.rect.top === 0 && element.sticky.container === document.querySelector('body')) {
219+
this.css(element, {
220+
position: 'fixed',
221+
top: element.sticky.rect.top + 'px',
222+
left: element.sticky.rect.left + 'px',
223+
width: element.sticky.rect.width + 'px'
224+
});
225+
} else if (this.scrollTop > element.sticky.rect.top - element.sticky.marginTop) {
225226
this.css(element, {
226227
position: 'fixed',
227228
width: element.sticky.rect.width + 'px',

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

34 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.1.4",
3+
"version": "1.1.5",
44
"description": "Sticky elements",
55
"main": "index.js",
66
"scripts": {

src/sticky.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Sticky.js
44
* Library for sticky elements written in vanilla javascript. With this library you can easily set sticky elements on your website. It's also responsive.
55
*
6-
* @version 1.1.4
6+
* @version 1.1.5
77
* @author Rafal Galus <[email protected]>
88
* @website https://rgalus.github.io/sticky-js/
99
* @repo https://github.com/rgalus/sticky-js
@@ -21,7 +21,7 @@ class Sticky {
2121
this.selector = selector;
2222
this.elements = [];
2323

24-
this.version = '1.1.4';
24+
this.version = '1.1.5';
2525

2626
this.vp = this.getViewportSize();
2727
this.scrollTop = this.getScrollTopPosition();
@@ -90,16 +90,8 @@ class Sticky {
9090
* @param {node} element - Element to be activated
9191
*/
9292
activate(element) {
93-
const heightBefore = element.sticky.container.offsetHeight;
94-
95-
this.css(element, { position: 'fixed' });
96-
97-
const heightAfter = element.sticky.container.offsetHeight;
98-
99-
this.css(element, { position: '' });
100-
10193
if (
102-
(heightAfter >= heightBefore)
94+
((element.sticky.rect.top + element.sticky.rect.height) < (element.sticky.container.rect.top + element.sticky.container.rect.height))
10395
&& (element.sticky.stickyFor < this.vp.width)
10496
&& !element.sticky.active
10597
) {
@@ -119,6 +111,8 @@ class Sticky {
119111
this.initScrollEvents(element);
120112
element.sticky.scrollEvent = true;
121113
}
114+
115+
this.setPosition(element);
122116
}
123117

124118

@@ -145,12 +139,14 @@ class Sticky {
145139
element.sticky.container.rect = this.getRectangle(element.sticky.container);
146140

147141
if (
148-
element.sticky.stickyFor < this.vp.width
142+
((element.sticky.rect.top + element.sticky.rect.height) < (element.sticky.container.rect.top + element.sticky.container.rect.height))
143+
&& (element.sticky.stickyFor < this.vp.width)
149144
&& !element.sticky.active
150145
) {
151146
element.sticky.active = true;
152147
} else if (
153-
element.sticky.stickyFor >= this.vp.width
148+
((element.sticky.rect.top + element.sticky.rect.height) >= (element.sticky.container.rect.top + element.sticky.container.rect.height))
149+
|| element.sticky.stickyFor >= this.vp.width
154150
&& element.sticky.active
155151
) {
156152
element.sticky.active = false;
@@ -201,7 +197,18 @@ class Sticky {
201197
element.sticky.rect = this.getRectangle(element);
202198
}
203199

204-
if (this.scrollTop > (element.sticky.rect.top - element.sticky.marginTop)) {
200+
201+
if (
202+
element.sticky.rect.top === 0
203+
&& element.sticky.container === document.querySelector('body')
204+
) {
205+
this.css(element, {
206+
position: 'fixed',
207+
top: element.sticky.rect.top + 'px',
208+
left: element.sticky.rect.left + 'px',
209+
width: element.sticky.rect.width + 'px',
210+
});
211+
} else if (this.scrollTop > (element.sticky.rect.top - element.sticky.marginTop)) {
205212
this.css(element, {
206213
position: 'fixed',
207214
width: element.sticky.rect.width + 'px',

0 commit comments

Comments
 (0)