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

Commit 891593b

Browse files
author
rgalus
committed
Merge branch 'develop'
2 parents 540f33b + 91bc585 commit 891593b

7 files changed

+30
-19
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: ~5.12kb, gzipped: ~1.45kb)
12+
- Lightweight (minified: ~5.28kb, gzipped: ~1.48kb)
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.5"
21+
"version": "1.1.6"
2222
}

dist/sticky.compile.js

+11-6
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.5
7+
* @version 1.1.6
88
* @author Rafal Galus <[email protected]>
99
* @website https://rgalus.github.io/sticky-js/
1010
* @repo https://github.com/rgalus/sticky-js
@@ -27,15 +27,17 @@ var Sticky = function () {
2727
this.selector = selector;
2828
this.elements = [];
2929

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

3232
this.vp = this.getViewportSize();
3333
this.scrollTop = this.getScrollTopPosition();
34+
this.body = document.querySelector('body');
3435

3536
this.options = {
3637
marginTop: options.marginTop || 0,
3738
stickyFor: options.stickFor || 0,
38-
stickyClass: options.stickyClass || null
39+
stickyClass: options.stickyClass || null,
40+
stickyContainer: options.stickyContainer || 'body'
3941
};
4042

4143
this.run();
@@ -82,6 +84,9 @@ var Sticky = function () {
8284
element.sticky.marginTop = parseInt(element.getAttribute('data-margin-top')) || this.options.marginTop;
8385
element.sticky.stickyFor = parseInt(element.getAttribute('data-sticky-for')) || this.options.stickyFor;
8486
element.sticky.stickyClass = element.getAttribute('data-sticky-class') || this.options.stickyClass;
87+
// @todo attribute for stickyContainer
88+
// element.sticky.stickyContainer = element.getAttribute('data-sticky-container') || this.options.stickyContainer;
89+
element.sticky.stickyContainer = this.options.stickyContainer;
8590

8691
element.sticky.container = this.getStickyContainer(element);
8792
element.sticky.container.rect = this.getRectangle(element.sticky.container);
@@ -215,7 +220,7 @@ var Sticky = function () {
215220
element.sticky.rect = this.getRectangle(element);
216221
}
217222

218-
if (element.sticky.rect.top === 0 && element.sticky.container === document.querySelector('body')) {
223+
if (element.sticky.rect.top === 0 && element.sticky.container === this.body) {
219224
this.css(element, {
220225
position: 'fixed',
221226
top: element.sticky.rect.top + 'px',
@@ -274,9 +279,9 @@ var Sticky = function () {
274279

275280

276281
Sticky.prototype.getStickyContainer = function getStickyContainer(element) {
277-
var container = element;
282+
var container = element.parentNode;
278283

279-
while (!container.hasAttribute('data-sticky-container') && container !== document.querySelector('body')) {
284+
while (!container.hasAttribute('data-sticky-container') && !container.parentNode.querySelector(element.sticky.stickyContainer) && container !== this.body) {
280285
container = container.parentNode;
281286
}
282287

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

37 Bytes
Binary file not shown.

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sticky-js",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
44
"description": "Sticky elements",
55
"main": "index.js",
66
"scripts": {
@@ -25,10 +25,10 @@
2525
},
2626
"homepage": "https://github.com/rgalus/sticky-js#readme",
2727
"devDependencies": {
28-
"babel-core": "^6.14.0",
29-
"babel-preset-es2015": "^6.14.0",
30-
"babel-register": "^6.14.0",
31-
"browser-sync": "^2.15.0",
28+
"babel-core": "^6.18.2",
29+
"babel-preset-es2015": "^6.18.0",
30+
"babel-register": "^6.18.0",
31+
"browser-sync": "^2.18.2",
3232
"del": "^2.2.2",
3333
"gulp": "github:gulpjs/gulp#4.0",
3434
"gulp-babel": "^6.1.2",

src/sticky.js

+11-5
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.5
6+
* @version 1.1.6
77
* @author Rafal Galus <[email protected]>
88
* @website https://rgalus.github.io/sticky-js/
99
* @repo https://github.com/rgalus/sticky-js
@@ -21,15 +21,17 @@ class Sticky {
2121
this.selector = selector;
2222
this.elements = [];
2323

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

2626
this.vp = this.getViewportSize();
2727
this.scrollTop = this.getScrollTopPosition();
28+
this.body = document.querySelector('body');
2829

2930
this.options = {
3031
marginTop: options.marginTop || 0,
3132
stickyFor: options.stickFor || 0,
3233
stickyClass: options.stickyClass || null,
34+
stickyContainer: options.stickyContainer || 'body',
3335
};
3436

3537
this.run();
@@ -68,6 +70,9 @@ class Sticky {
6870
element.sticky.marginTop = parseInt(element.getAttribute('data-margin-top')) || this.options.marginTop;
6971
element.sticky.stickyFor = parseInt(element.getAttribute('data-sticky-for')) || this.options.stickyFor;
7072
element.sticky.stickyClass = element.getAttribute('data-sticky-class') || this.options.stickyClass;
73+
// @todo attribute for stickyContainer
74+
// element.sticky.stickyContainer = element.getAttribute('data-sticky-container') || this.options.stickyContainer;
75+
element.sticky.stickyContainer = this.options.stickyContainer;
7176

7277
element.sticky.container = this.getStickyContainer(element);
7378
element.sticky.container.rect = this.getRectangle(element.sticky.container);
@@ -200,7 +205,7 @@ class Sticky {
200205

201206
if (
202207
element.sticky.rect.top === 0
203-
&& element.sticky.container === document.querySelector('body')
208+
&& element.sticky.container === this.body
204209
) {
205210
this.css(element, {
206211
position: 'fixed',
@@ -260,11 +265,12 @@ class Sticky {
260265
* @return {node} element - Sticky container
261266
*/
262267
getStickyContainer(element) {
263-
let container = element;
268+
let container = element.parentNode;
264269

265270
while (
266271
!container.hasAttribute('data-sticky-container')
267-
&& container !== document.querySelector('body')
272+
&& !container.parentNode.querySelector(element.sticky.stickyContainer)
273+
&& container !== this.body
268274
) {
269275
container = container.parentNode;
270276
}

0 commit comments

Comments
 (0)