Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add limitX and limitY with range for each layer #267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/parallax.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ class Parallax {
this.depthsX = []
this.depthsY = []

this.limitsX = []
this.limitsY = []

for (let index = 0; index < this.layers.length; index++) {
let layer = this.layers[index]

Expand All @@ -310,6 +313,12 @@ class Parallax {
let depth = helpers.data(layer, 'depth') || 0
this.depthsX.push(helpers.data(layer, 'depth-x') || depth)
this.depthsY.push(helpers.data(layer, 'depth-y') || depth)

const limitX = (helpers.data(layer, 'limit-x') || '').split(',');
this.limitsX.push(limitX)

const limitY = (helpers.data(layer, 'limit-y') || '').split(',');
this.limitsY.push(limitY)
}
}

Expand Down Expand Up @@ -489,6 +498,15 @@ class Parallax {
depthY = this.depthsY[index],
xOffset = this.velocityX * (depthX * (this.invertX ? -1 : 1)),
yOffset = this.velocityY * (depthY * (this.invertY ? -1 : 1))

if (!isNaN(parseFloat(this.limitsX[index][0])) && !isNaN(parseFloat(this.limitsX[index][1]))) {
xOffset = helpers.clamp(xOffset, parseFloat(this.limitsX[index][0]), parseFloat(this.limitsX[index][1]))
}

if (!isNaN(parseFloat(this.limitsY[index][0])) && !isNaN(parseFloat(this.limitsY[index][1]))) {
yOffset = helpers.clamp(yOffset, parseFloat(this.limitsY[index][0]), parseFloat(this.limitsY[index][1]))
}

this.setPosition(layer, xOffset, yOffset)
}
this.raf = rqAnFr(this.onAnimationFrame)
Expand Down