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

Commit 589fade

Browse files
author
rgalus
committed
1.1.8
1 parent a025fd7 commit 589fade

6 files changed

+46
-10
lines changed

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.7"
21+
"version": "1.1.8"
2222
}

dist/sticky.compile.js

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

30-
this.version = '1.1.7';
30+
this.version = '1.1.8';
3131

3232
this.vp = this.getViewportSize();
3333
this.scrollTop = this.getScrollTopPosition();
3434
this.body = document.querySelector('body');
3535

3636
this.options = {
37+
wrap: options.wrap || false,
3738
marginTop: options.marginTop || 0,
3839
stickyFor: options.stickyFor || 0,
3940
stickyClass: options.stickyClass || null,
@@ -84,6 +85,7 @@ var Sticky = function () {
8485
element.sticky.marginTop = parseInt(element.getAttribute('data-margin-top')) || this.options.marginTop;
8586
element.sticky.stickyFor = parseInt(element.getAttribute('data-sticky-for')) || this.options.stickyFor;
8687
element.sticky.stickyClass = element.getAttribute('data-sticky-class') || this.options.stickyClass;
88+
element.sticky.wrap = element.hasAttribute('data-sticky-wrap') ? true : this.options.wrap;
8789
// @todo attribute for stickyContainer
8890
// element.sticky.stickyContainer = element.getAttribute('data-sticky-container') || this.options.stickyContainer;
8991
element.sticky.stickyContainer = this.options.stickyContainer;
@@ -100,10 +102,26 @@ var Sticky = function () {
100102
};
101103
}
102104

105+
if (element.sticky.wrap) {
106+
this.wrapElement(element);
107+
}
108+
103109
// activate rendered element
104110
this.activate(element);
105111
};
106112

113+
/**
114+
* Wraps element into placeholder element
115+
* @function
116+
* @param {node} element - Element to be wrapped
117+
*/
118+
119+
120+
Sticky.prototype.wrapElement = function wrapElement(element) {
121+
element.insertAdjacentHTML('beforebegin', '<span></span>');
122+
element.previousSibling.appendChild(element);
123+
};
124+
107125
/**
108126
* Function that activates element when specified conditions are met and then initalise events
109127
* @function
@@ -220,6 +238,14 @@ var Sticky = function () {
220238
element.sticky.rect = this.getRectangle(element);
221239
}
222240

241+
if (element.sticky.wrap) {
242+
this.css(element.parentNode, {
243+
display: 'block',
244+
width: element.sticky.rect.width + 'px',
245+
height: element.sticky.rect.height + 'px'
246+
});
247+
}
248+
223249
if (element.sticky.rect.top === 0 && element.sticky.container === this.body) {
224250
this.css(element, {
225251
position: 'fixed',
@@ -236,19 +262,29 @@ var Sticky = function () {
236262

237263
if (this.scrollTop + element.sticky.rect.height + element.sticky.marginTop > element.sticky.container.rect.top + element.sticky.container.offsetHeight) {
238264

239-
if (element.sticky.stickyClass) element.classList.remove(element.sticky.stickyClass);
265+
if (element.sticky.stickyClass) {
266+
element.classList.remove(element.sticky.stickyClass);
267+
}
240268

241269
this.css(element, {
242270
top: element.sticky.container.rect.top + element.sticky.container.offsetHeight - (this.scrollTop + element.sticky.rect.height) + 'px' });
243271
} else {
244-
if (element.sticky.stickyClass) element.classList.add(element.sticky.stickyClass);
272+
if (element.sticky.stickyClass) {
273+
element.classList.add(element.sticky.stickyClass);
274+
}
245275

246276
this.css(element, { top: element.sticky.marginTop + 'px' });
247277
}
248278
} else {
249-
if (element.sticky.stickyClass) element.classList.remove(element.sticky.stickyClass);
279+
if (element.sticky.stickyClass) {
280+
element.classList.remove(element.sticky.stickyClass);
281+
}
250282

251283
this.css(element, { position: '', width: '', top: '', left: '' });
284+
285+
if (element.sticky.wrap) {
286+
this.css(element.parentNode, { display: '', width: '', height: '' });
287+
}
252288
}
253289
};
254290

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

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

src/sticky.js

+2-2
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.7
6+
* @version 1.1.8
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.7';
24+
this.version = '1.1.8';
2525

2626
this.vp = this.getViewportSize();
2727
this.scrollTop = this.getScrollTopPosition();

0 commit comments

Comments
 (0)