Skip to content

Commit b6a994f

Browse files
committed
✨ add new customWrapper setting #39
1 parent 9132ae2 commit b6a994f

File tree

7 files changed

+165
-48
lines changed

7 files changed

+165
-48
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ orientation | String | up | up - right - down - left - up left - up right - down
8282
scale | Number | 1.3 | need to be above 1
8383
overflow | Boolean | false |
8484
delay | Number | 0.4 | the delay is in second
85-
transition | String | false | any CSS transition
85+
transition | String | '' | any CSS transition
8686
customContainer | String or Node | false |
87+
customWrapper | string | '' | the selector of the custom wrapper
8788
maxTransition | Number | 0 | it should be a percentage between 1 and 99
8889

8990
You can apply these settings with the following JS code:
@@ -95,15 +96,16 @@ new simpleParallax(images, {
9596
orientation: 'down',
9697
scale: 1.3,
9798
overflow: true,
98-
customContainer: '.container'
99+
customContainer: '.container',
100+
customWrapper: '.wrapper'
99101
});
100102
```
101103

102104
### orientation - *String* - see [example](https://simpleparallax.com#example-orientation)
103-
This is the orientation (or direction) of the parallax effect. Choose *up* and when scrolling down, the image will translate from the bottom to the top (so the image will translate up). When scrolling up, the image will translate from the top to the bottom. Same logic apply for all others orientations (*right*, *down*, *left*, *up left*, *up right*, *down left* or *down right*). When 2 directions are combined (for example *down right*), the image will translate diagonally.
105+
This is the orientation (or direction) of the parallax effect. Choose *up* and when scrolling down, the image will translate from the bottom to the top (so the image will translate up). When scrolling up, the image will translate from the top to the bottom. Same logic applies for all others orientations (*right*, *down*, *left*, *up left*, *up right*, *down left* or *down right*). When 2 directions are combined (for example *down right*), the image will translate diagonally.
104106

105107
### scale - *Number* - see [example](https://simpleparallax.com#example-scale)
106-
The higher the scale is set, the more visible the parallax effect will be. In return, the image will lose in quality. To reduce the lossless effect, if the scale is set at 1.5 and your image is 500px width, do the simple math 500 * 1.5 = 750. So you can choose a 750px image to replace your 500px one, and don't see any quality leak. More information are available if you read the case study [here](https://medium.com/@geoffrey.signorato/case-study-create-a-parallax-effect-directly-on-img-tags-with-javascript-35b8daf81471).
108+
The higher the scale is set, the more visible the parallax effect will be. In return, the image will lose in quality. To reduce the lossless effect, if the scale is set at 1.5 and your image is 500px width, do the simple math 500 * 1.5 = 750. So you can choose a 750px image to replace your 500px one, and don't see any quality leak. More information is available if you read the case study [here](https://medium.com/@geoffrey.signorato/case-study-create-a-parallax-effect-directly-on-img-tags-with-javascript-35b8daf81471).
107109

108110
### overflow - *Boolean* - see [example](https://simpleparallax.com#example-overflow)
109111
By default, the image is scaled to apply a parallax effect without any overflow on the layout - you can check the [case study](https://medium.com/@geoffrey.signorato/case-study-create-a-parallax-effect-directly-on-img-tags-with-javascript-35b8daf81471) to have a better understanding. When *overflow* is set to true, the image will translate out of its natural flow (so it may overlap with your content).
@@ -112,11 +114,14 @@ By default, the image is scaled to apply a parallax effect without any overflow
112114
When a *delay* is set, the translation of the image will continue during that delay when the user stops scrolling. That gives a very nice effect. The delay is in second.
113115

114116
### transition - *String* - see [example](https://simpleparallax.com#example-delay-transition)
115-
The *transition* setting works closely with the *delay* setting. This setting will add any CSS transition to the delay setting. For example you can use *ease* or *ease-in-out*.
117+
The *transition* setting works closely with the *delay* setting. This setting will add any CSS transition to the delay setting. For example, you can use *ease* or *ease-in-out*.
116118

117119
### customContainer - *String or Node*
118120
By default, the parallax calculation is done with the body scroll percentage. In some cases, the images may be in a container that has its own scroll area, so to have an accurate calculation, the custom container should be set.
119121

122+
### customWrapper - *string*
123+
In some cases, you want to use your own wrapper instead of the one created by the plugin. If you specify your custom wrapper, the plugin will add the "simpleParallax" class along with an "overflow: hidden" style.
124+
120125
### maxTransition - *Number* - see [example](https://simpleparallax.com#example-max-transition)
121126
The maxTransition setting should be used to stop the parallax animation after a given percentage. By default, it translates from 0% to 100% of the user viewport. You can change it here to any percentage you want.
122127

dist/simpleParallax.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* simpleParallax - simpleParallax is a simple JavaScript library that gives your website parallax animations on any images,
3-
* @date: 01-02-2020 23:1:26,
3+
* @date: 07-04-2020 18:47:4,
44
* @version: 5.3.0,
55
* @link: https://simpleparallax.com/
66
*/
@@ -289,23 +289,42 @@ function () {
289289
}, {
290290
key: "wrapElement",
291291
value: function wrapElement() {
292-
// check is current image is in a <picture> tag
292+
// get the customWrapper if any
293+
var customWrapper = this.settings.customWrapper && this.element.closest(this.settings.customWrapper); // check is current image is in a <picture> tag
294+
293295
var elementToWrap = this.element.closest('picture') || this.element; // create a .simpleParallax wrapper container
294296

295-
var wrapper = document.createElement('div');
297+
var wrapper = document.createElement('div'); //if there is a custom wrapper
298+
//override the wrapper with it
299+
300+
if (customWrapper) {
301+
wrapper = this.element.closest(this.settings.customWrapper);
302+
}
303+
296304
wrapper.classList.add('simpleParallax');
297305
wrapper.style.overflow = 'hidden'; // append the image inside the new wrapper
298306

299-
elementToWrap.parentNode.insertBefore(wrapper, elementToWrap);
300-
wrapper.appendChild(elementToWrap);
307+
if (!customWrapper) {
308+
elementToWrap.parentNode.insertBefore(wrapper, elementToWrap);
309+
wrapper.appendChild(elementToWrap);
310+
}
311+
301312
this.elementContainer = wrapper;
302313
} // unwrap the element from .simpleParallax wrapper container
303314

304315
}, {
305316
key: "unWrapElement",
306317
value: function unWrapElement() {
307-
var wrapper = this.elementContainer;
308-
wrapper.replaceWith.apply(wrapper, _toConsumableArray(wrapper.childNodes));
318+
var wrapper = this.elementContainer; // get the customWrapper if any
319+
320+
var customWrapper = this.settings.customWrapper && this.element.closest(this.settings.customWrapper); //if there is a custom wrapper, we jusy need to remove the class and style
321+
322+
if (customWrapper) {
323+
wrapper.classList.remove('simpleParallax');
324+
wrapper.style.overflow = '';
325+
} else {
326+
wrapper.replaceWith.apply(wrapper, _toConsumableArray(wrapper.childNodes));
327+
}
309328
} // apply default style on element
310329

311330
}, {

dist/simpleParallax.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
.container {
1010
display: flex;
1111
align-items: center;
12-
flex-wrap: wrap;
13-
max-width: 900px;
14-
margin: 600px auto;
12+
flex-direction: column;
1513
/* max-height: 400px;
1614
overflow: auto; */
1715
}
1816

19-
.container > div {
17+
/* .container > div {
2018
flex: 100%;
21-
}
22-
19+
} */
20+
/* div{
21+
overflow: hidden;
22+
} */
2323
img {
24-
max-width: 100%;
24+
max-width: 850px;
2525
height: auto;
2626
}
2727

@@ -41,7 +41,13 @@
4141
<div>
4242
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
4343
</div>
44+
<div class="customWrapper">
45+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
46+
</div>
4447
<div>
48+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
49+
</div>
50+
<!-- <div>
4551
<picture>
4652
<source media="(min-width: 650px)" srcset="https://simpleparallax.com/images/image03.jpg" />
4753
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
@@ -212,6 +218,67 @@
212218
<div>
213219
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
214220
</div>
221+
<div>
222+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
223+
</div> <div>
224+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
225+
</div> <div>
226+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
227+
</div> <div>
228+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
229+
</div> <div>
230+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
231+
</div> <div>
232+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
233+
</div> <div>
234+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
235+
</div> <div>
236+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
237+
</div> <div>
238+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
239+
</div> <div>
240+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
241+
</div> <div>
242+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
243+
</div> <div>
244+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
245+
</div> <div>
246+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
247+
</div> <div>
248+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
249+
</div> <div>
250+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
251+
</div> <div>
252+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
253+
</div> <div>
254+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
255+
</div> <div>
256+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
257+
</div> <div>
258+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
259+
</div> <div>
260+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
261+
</div> <div>
262+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
263+
</div> <div>
264+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
265+
</div> <div>
266+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
267+
</div> <div>
268+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
269+
</div> <div>
270+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
271+
</div> <div>
272+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
273+
</div> <div>
274+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
275+
</div> <div>
276+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
277+
</div> <div>
278+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
279+
</div> <div>
280+
<img class="up" src="https://simpleparallax.com/images/image03.jpg" alt="simpleParallax image" />
281+
</div> -->
215282
</section>
216283
</body>
217284
</html>

package-lock.json

Lines changed: 17 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/instances/parallax.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,47 @@ class ParallaxInstance {
6666
// if overflow option is set to false
6767
// wrap the element into a .simpleParallax div and apply overflow hidden to hide the image excedant (result of the scale)
6868
wrapElement() {
69+
// get the customWrapper if any
70+
let customWrapper = (this.settings.customWrapper && this.element.closest(this.settings.customWrapper));
71+
6972
// check is current image is in a <picture> tag
7073
const elementToWrap = this.element.closest('picture') || this.element;
7174

7275
// create a .simpleParallax wrapper container
73-
const wrapper = document.createElement('div');
76+
let wrapper = document.createElement('div');
77+
78+
//if there is a custom wrapper
79+
//override the wrapper with it
80+
if (customWrapper) {
81+
wrapper = this.element.closest(this.settings.customWrapper);
82+
}
83+
7484
wrapper.classList.add('simpleParallax');
7585
wrapper.style.overflow = 'hidden';
7686

7787
// append the image inside the new wrapper
78-
elementToWrap.parentNode.insertBefore(wrapper, elementToWrap);
79-
wrapper.appendChild(elementToWrap);
88+
if (!customWrapper) {
89+
elementToWrap.parentNode.insertBefore(wrapper, elementToWrap);
90+
wrapper.appendChild(elementToWrap);
91+
}
8092

8193
this.elementContainer = wrapper;
8294
}
8395

8496
// unwrap the element from .simpleParallax wrapper container
8597
unWrapElement() {
8698
const wrapper = this.elementContainer;
87-
wrapper.replaceWith(...wrapper.childNodes);
99+
100+
// get the customWrapper if any
101+
let customWrapper = (this.settings.customWrapper && this.element.closest(this.settings.customWrapper));
102+
103+
//if there is a custom wrapper, we jusy need to remove the class and style
104+
if (customWrapper) {
105+
wrapper.classList.remove('simpleParallax');
106+
wrapper.style.overflow = '';
107+
} else {
108+
wrapper.replaceWith(...wrapper.childNodes);
109+
}
88110
}
89111

90112
// apply default style on element

test/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
import SimpleParallax from '../src/simpleParallax';
1+
import simpleParallax from '../src/simpleParallax';
2+
// var simpleParallax = require('../dist/simpleParallax');
3+
24

35
// let images = document.querySelectorAll('img'),
46
// instance;
57

68
let instanceUp;
79
const optionUp = {
810
orientation: 'up',
9-
maxTransition: 50,
1011
scale: 1.5,
11-
overflow: true
12+
customWrapper: '.customWrapper'
1213
// customContainer: document.querySelector('.container')
1314
};
1415
const imageUp = document.getElementsByTagName('img');
1516

1617
const images = document.querySelectorAll('img');
1718
// images.forEach((image) => {
18-
new SimpleParallax(images, optionUp);
19+
const instance = new simpleParallax(images, optionUp);
1920
// });
2021

22+
// setTimeout(() => {
23+
// instance.destroy();
24+
// }, 3000);
25+
26+
2127
// instanceUp = new SimpleParallax('img', optionUp);
2228

2329
// let instanceDown,

0 commit comments

Comments
 (0)