Skip to content
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
37 changes: 33 additions & 4 deletions src/parallax.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ const MAGIC_NUMBER = 30,
DEFAULTS = {
relativeInput: false,
clipRelativeInput: false,
tilt: false,
tiltPerspective: 2000,
tiltScale: 1,
inputElement: null,
hoverOnly: false,
calibrationThreshold: 100,
Expand All @@ -149,7 +152,8 @@ const MAGIC_NUMBER = 30,
pointerEvents: false,
precision: 1,
onReady: null,
selector: null
selector: null,
continueAnimatingWhileDisabled: false,
}

class Parallax {
Expand All @@ -174,9 +178,13 @@ class Parallax {
precision: helpers.data(this.element, 'precision'),
relativeInput: helpers.data(this.element, 'relative-input'),
clipRelativeInput: helpers.data(this.element, 'clip-relative-input'),
tilt: helpers.data(this.element, 'tilt'),
tiltPerspective: helpers.data(this.element, 'tilt-perspective'),
tiltScale: helpers.data(this.element, 'tilt-scale'),
hoverOnly: helpers.data(this.element, 'hover-only'),
inputElement: document.querySelector(helpers.data(this.element, 'input-element')),
selector: helpers.data(this.element, 'selector')
selector: helpers.data(this.element, 'selector'),
continueAnimatingWhileDisabled: helpers.data(this.element, 'continue-animating-while-disabled')
}

for (let key in data) {
Expand Down Expand Up @@ -380,7 +388,10 @@ class Parallax {
}

window.removeEventListener('resize', this.onWindowResize)
rqAnFr.cancel(this.raf)

if (!this.continueAnimatingWhileDisabled) {
rqAnFr.cancel(this.raf)
}
}

calibrate(x, y) {
Expand Down Expand Up @@ -431,6 +442,20 @@ class Parallax {
}
}

setTransform(element, x, y) {
var perspective = this.tiltPerspective;
var scale = this.tiltScale;
element.style.transform = "perspective(" + perspective + "px) " +
"rotateX(" + y + "deg) " +
"rotateY(" + -x + "deg) " +
"scale3d(" + scale + ", " + scale + ", " + scale + ")";
}

resetTransform() {
this.inputX = this.calibrationX;
this.inputY = this.calibrationY;
}

onOrientationTimer() {
if (this.orientationSupport && this.orientationStatus === 0) {
this.disable()
Expand Down Expand Up @@ -489,7 +514,11 @@ class Parallax {
depthY = this.depthsY[index],
xOffset = this.velocityX * (depthX * (this.invertX ? -1 : 1)),
yOffset = this.velocityY * (depthY * (this.invertY ? -1 : 1))
this.setPosition(layer, xOffset, yOffset)
if (this.tilt) {
this.setTransform(layer, xOffset, yOffset);
} else {
this.setPosition(layer, xOffset, yOffset);
}
}
this.raf = rqAnFr(this.onAnimationFrame)
}
Expand Down