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

Commit a025fd7

Browse files
author
rgalus
committed
Merge branch 'develop'
2 parents 5f1c993 + c5f0190 commit a025fd7

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
[![npm version](https://badge.fury.io/js/sticky-js.svg)](https://badge.fury.io/js/sticky-js)
33
[![Bower version](https://badge.fury.io/bo/sticky.js.svg)](https://badge.fury.io/bo/sticky.js)
44

5-
[DEMO](https://rgalus.github.io/sticky-js/)
5+
> Sticky-js is a 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-
Sticky-js is a library for sticky elements written in vanilla javascript. With this library you can easily set sticky elements on your website. It's also responsive.
7+
[DEMO](https://rgalus.github.io/sticky-js/)
88

99
## Features
1010

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

@@ -87,13 +87,15 @@ Update sticky, e.g. when parent container (data-sticky-container) change height
8787
```js
8888
var sticky = new Sticky('.sticky');
8989

90+
// and when parent change height..
9091
sticky.update();
9192
```
9293

9394
## Available options
9495

9596
Option | Type | Default | Description
9697
------ | ---- | ------- | ----
98+
data-sticky-wrap | boolean | false | When it's `true` sticky element is wrapped in `<span></span>` which has sticky element dimensions. Prevents content from "jumping".
9799
data-margin-top | number | 0 | Margin between page and sticky element when scrolled
98100
data-sticky-for | number | 0 | Breakpoint which when is bigger than viewport width, sticky is activated and when is smaller, then sticky is destroyed
99101
data-sticky-class | string | null | Class added to sticky element when it is stuck

demo/index.html

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
}
3131

3232
img {
33+
display: block;
3334
z-index: 2;
3435
}
3536

@@ -66,7 +67,7 @@
6667
margin-top: 200px;
6768
background: #ecf0f1;
6869

69-
height: 500px;
70+
min-height: 500px;
7071
}
7172

7273
</style>
@@ -91,7 +92,10 @@
9192
<section class="container" data-sticky-container>
9293
<div class="row column">
9394
<img src="http://placehold.it/250x250" class="float-left" alt="Sticky" data-sticky data-sticky-for="1023" data-sticky-class="is-sticky" data-margin-top="100">
94-
<img src="http://placehold.it/250x250" class="float-right" alt="Sticky" data-sticky data-margin-top="100">
95+
<div class="float-right">
96+
<img src="http://placehold.it/250x250" alt="Sticky" data-sticky data-sticky-wrap data-margin-top="100">
97+
<img src="http://placehold.it/250x125/ecf0f1?text=Not+sticky" alt="Not sticky">
98+
</div>
9599
</div>
96100
</section>
97101

src/sticky.js

+37-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Sticky {
2828
this.body = document.querySelector('body');
2929

3030
this.options = {
31+
wrap: options.wrap || false,
3132
marginTop: options.marginTop || 0,
3233
stickyFor: options.stickyFor || 0,
3334
stickyClass: options.stickyClass || null,
@@ -70,6 +71,7 @@ class Sticky {
7071
element.sticky.marginTop = parseInt(element.getAttribute('data-margin-top')) || this.options.marginTop;
7172
element.sticky.stickyFor = parseInt(element.getAttribute('data-sticky-for')) || this.options.stickyFor;
7273
element.sticky.stickyClass = element.getAttribute('data-sticky-class') || this.options.stickyClass;
74+
element.sticky.wrap = element.hasAttribute('data-sticky-wrap') ? true : this.options.wrap;
7375
// @todo attribute for stickyContainer
7476
// element.sticky.stickyContainer = element.getAttribute('data-sticky-container') || this.options.stickyContainer;
7577
element.sticky.stickyContainer = this.options.stickyContainer;
@@ -84,11 +86,26 @@ class Sticky {
8486
element.onload = () => element.sticky.rect = this.getRectangle(element);
8587
}
8688

89+
if (element.sticky.wrap) {
90+
this.wrapElement(element);
91+
}
92+
8793
// activate rendered element
8894
this.activate(element);
8995
}
9096

9197

98+
/**
99+
* Wraps element into placeholder element
100+
* @function
101+
* @param {node} element - Element to be wrapped
102+
*/
103+
wrapElement(element) {
104+
element.insertAdjacentHTML('beforebegin', '<span></span>');
105+
element.previousSibling.appendChild(element);
106+
}
107+
108+
92109
/**
93110
* Function that activates element when specified conditions are met and then initalise events
94111
* @function
@@ -202,6 +219,13 @@ class Sticky {
202219
element.sticky.rect = this.getRectangle(element);
203220
}
204221

222+
if (element.sticky.wrap) {
223+
this.css(element.parentNode, {
224+
display: 'block',
225+
width: element.sticky.rect.width + 'px',
226+
height: element.sticky.rect.height + 'px',
227+
});
228+
}
205229

206230
if (
207231
element.sticky.rect.top === 0
@@ -225,20 +249,30 @@ class Sticky {
225249
> (element.sticky.container.rect.top + element.sticky.container.offsetHeight)
226250
) {
227251

228-
if (element.sticky.stickyClass) element.classList.remove(element.sticky.stickyClass);
252+
if (element.sticky.stickyClass) {
253+
element.classList.remove(element.sticky.stickyClass);
254+
}
229255

230256
this.css(element, {
231257
top: (element.sticky.container.rect.top + element.sticky.container.offsetHeight) - (this.scrollTop + element.sticky.rect.height) + 'px' }
232258
);
233259
} else {
234-
if (element.sticky.stickyClass) element.classList.add(element.sticky.stickyClass);
260+
if (element.sticky.stickyClass) {
261+
element.classList.add(element.sticky.stickyClass);
262+
}
235263

236264
this.css(element, { top: element.sticky.marginTop + 'px' });
237265
}
238266
} else {
239-
if (element.sticky.stickyClass) element.classList.remove(element.sticky.stickyClass);
267+
if (element.sticky.stickyClass) {
268+
element.classList.remove(element.sticky.stickyClass);
269+
}
240270

241271
this.css(element, { position: '', width: '', top: '', left: '' });
272+
273+
if (element.sticky.wrap) {
274+
this.css(element.parentNode, { display: '', width: '', height: '' });
275+
}
242276
}
243277
}
244278

yarn.lock

+10-10
Original file line numberDiff line numberDiff line change
@@ -1582,15 +1582,15 @@ [email protected]:
15821582
vinyl "^1.0.0"
15831583

15841584
gulp-uglify@^2.0.0:
1585-
version "2.0.0"
1586-
resolved "https://registry.yarnpkg.com/gulp-uglify/-/gulp-uglify-2.0.0.tgz#cbe4aae4fe0b6bdd760335bc46f200fff699c4af"
1585+
version "2.0.1"
1586+
resolved "https://registry.yarnpkg.com/gulp-uglify/-/gulp-uglify-2.0.1.tgz#e8cfb831014fc9ff2e055e33785861830d499365"
15871587
dependencies:
15881588
gulplog "^1.0.0"
15891589
has-gulplog "^0.1.0"
15901590
lodash "^4.13.1"
15911591
make-error-cause "^1.1.1"
15921592
through2 "^2.0.0"
1593-
uglify-js "2.7.0"
1593+
uglify-js "2.7.5"
15941594
uglify-save-license "^0.4.1"
15951595
vinyl-sourcemaps-apply "^0.2.0"
15961596

@@ -1978,8 +1978,8 @@ jodid25519@^1.0.0:
19781978
jsbn "~0.1.0"
19791979

19801980
js-tokens@^3.0.0:
1981-
version "3.0.0"
1982-
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.0.tgz#a2f2a969caae142fb3cd56228358c89366957bd1"
1981+
version "3.0.1"
1982+
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"
19831983

19841984
jsbn@~0.1.0:
19851985
version "0.1.0"
@@ -3214,8 +3214,8 @@ [email protected]:
32143214
socket.io-parser "2.3.1"
32153215

32163216
source-map-support@^0.4.2:
3217-
version "0.4.10"
3218-
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.10.tgz#d7b19038040a14c0837a18e630a196453952b378"
3217+
version "0.4.11"
3218+
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.11.tgz#647f939978b38535909530885303daf23279f322"
32193219
dependencies:
32203220
source-map "^0.5.3"
32213221

@@ -3455,9 +3455,9 @@ [email protected]:
34553455
version "0.7.12"
34563456
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.12.tgz#04c81a99bdd5dc52263ea29d24c6bf8d4818a4bb"
34573457

3458-
3459-
version "2.7.0"
3460-
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.7.0.tgz#f021e38ba2ca740860f5bd5c695c2a817345f0ec"
3458+
3459+
version "2.7.5"
3460+
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.7.5.tgz#4612c0c7baaee2ba7c487de4904ae122079f2ca8"
34613461
dependencies:
34623462
async "~0.2.6"
34633463
source-map "~0.5.1"

0 commit comments

Comments
 (0)