Skip to content

Commit 635788c

Browse files
✨ Custom scroll container option #32
1 parent d65531d commit 635788c

File tree

5 files changed

+57
-35
lines changed

5 files changed

+57
-35
lines changed

dist/simpleParallax.js

Lines changed: 29 additions & 18 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: 06-12-2019 13:53:55,
3+
* @date: 06-12-2019 17:9:59,
44
* @version: 5.1.0,
55
* @link: https://simpleparallax.com/
66
*/
@@ -129,8 +129,9 @@ function () {
129129

130130
_createClass(Viewport, [{
131131
key: "setViewportTop",
132-
value: function setViewportTop() {
133-
this.positions.top = window.pageYOffset;
132+
value: function setViewportTop(container) {
133+
//if this is a custom container, user the scrollTop
134+
this.positions.top = container ? container.scrollTop : window.pageYOffset;
134135
return this.positions;
135136
}
136137
}, {
@@ -139,18 +140,14 @@ function () {
139140
this.positions.bottom = this.positions.top + this.positions.height;
140141
return this.positions;
141142
}
142-
}, {
143-
key: "setViewportHeight",
144-
value: function setViewportHeight() {
145-
this.positions.height = document.documentElement.clientHeight;
146-
return this.positions;
147-
}
148143
}, {
149144
key: "setViewportAll",
150-
value: function setViewportAll() {
151-
this.positions.top = window.pageYOffset;
145+
value: function setViewportAll(container) {
146+
//if this is a custom container, user the scrollTop
147+
this.positions.top = container ? container.scrollTop : window.pageYOffset; //if this is a custom container, get the height from the custom container itself
148+
149+
this.positions.height = container ? document.querySelector('.container').clientHeight : document.documentElement.clientHeight;
152150
this.positions.bottom = this.positions.top + this.positions.height;
153-
this.positions.height = document.documentElement.clientHeight;
154151
return this.positions;
155152
}
156153
}]);
@@ -347,7 +344,14 @@ function () {
347344

348345
this.elementHeight = positions.height; // get offset top
349346

350-
this.elementTop = positions.top + simpleParallax_viewport.positions.top; // get offset bottom
347+
this.elementTop = positions.top + simpleParallax_viewport.positions.top; //if there is a custom container
348+
349+
if (this.settings.customContainer) {
350+
//we need to do some calculation to get the position from the parent rather than the viewport
351+
var parentPositions = document.querySelector(this.settings.customContainer).getBoundingClientRect();
352+
this.elementTop = positions.top - parentPositions.top + simpleParallax_viewport.positions.top;
353+
} // get offset bottom
354+
351355

352356
this.elementBottom = this.elementHeight + this.elementTop;
353357
} // build the Threshold array to cater change for every pixel scrolled
@@ -508,11 +512,17 @@ function () {
508512
orientation: 'up',
509513
scale: 1.3,
510514
overflow: false,
511-
transition: 'cubic-bezier(0,0,0,1)'
515+
transition: 'cubic-bezier(0,0,0,1)',
516+
customContainer: false
512517
};
513518
this.settings = Object.assign(this.defaults, options); // check if the browser handle the Intersection Observer API
514519

515520
if (!('IntersectionObserver' in window)) intersectionObserverAvailable = false;
521+
522+
if (this.settings.customContainer) {
523+
this.customContainer = document.querySelector(this.settings.customContainer);
524+
}
525+
516526
this.lastPosition = -1;
517527
this.resizeIsDone = this.resizeIsDone.bind(this);
518528
this.handleResize = this.handleResize.bind(this);
@@ -523,7 +533,7 @@ function () {
523533
simpleParallax_createClass(SimpleParallax, [{
524534
key: "init",
525535
value: function init() {
526-
simpleParallax_viewport.setViewportAll();
536+
simpleParallax_viewport.setViewportAll(this.customContainer);
527537

528538
for (var i = this.elements.length - 1; i >= 0; i--) {
529539
var instance = new parallax(this.elements[i], this.settings);
@@ -552,7 +562,7 @@ function () {
552562
key: "handleResize",
553563
value: function handleResize() {
554564
// re-get all the viewport positions
555-
simpleParallax_viewport.setViewportAll();
565+
simpleParallax_viewport.setViewportAll(this.customContainer);
556566

557567
for (var i = instancesLength - 1; i >= 0; i--) {
558568
// re-get the current element offset
@@ -569,7 +579,7 @@ function () {
569579
key: "proceedRequestAnimationFrame",
570580
value: function proceedRequestAnimationFrame() {
571581
// get the offset top of the viewport
572-
simpleParallax_viewport.setViewportTop();
582+
simpleParallax_viewport.setViewportTop(this.customContainer);
573583

574584
if (this.lastPosition === simpleParallax_viewport.positions.top) {
575585
// if last position if the same than the curent one
@@ -595,9 +605,10 @@ function () {
595605
key: "proceedElement",
596606
value: function proceedElement(instance) {
597607
var isVisible = false; // is not support for Intersection Observer API
608+
// or if this is a custom container
598609
// use old function to check if element visible
599610

600-
if (!intersectionObserverAvailable) {
611+
if (!intersectionObserverAvailable || this.customContainer) {
601612
isVisible = instance.checkIfVisible(); // if support
602613
// use response from Intersection Observer API Callback
603614
} else {

0 commit comments

Comments
 (0)