Created
September 15, 2017 00:22
-
-
Save JulianJorgensen/d70d5b33bf30cadae26f94a455053b3c to your computer and use it in GitHub Desktop.
ParallaxController.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _setParallaxStyles(element) { | |
const top = element.attributes.top - scrollY; | |
const { totalDist } = element.attributes; | |
// Percent the element has moved based on current and total distance to move | |
const percentMoved = (top * -1 + windowHeight) / totalDist * 100; | |
// Scale percentMoved to min/max percent determined by offset props | |
const { slowerScrollRate, fadeSpeed } = element.props; | |
// Get the parallax X and Y offsets | |
const offsets = getParallaxOffsets( | |
element.offsets, | |
percentMoved, | |
slowerScrollRate | |
); | |
console.log('fadespeed', fadeSpeed); | |
const opacity = Math.pow(percentMoved, fadeSpeed) / 100; | |
// Apply styles | |
const el = element.elInner; | |
el.style.cssText = `position:relative; | |
transform:translate3d(${offsets.x.value}${offsets.x.unit}, ${offsets.y.value}${offsets.y.unit}, 0); | |
${fadeSpeed ? `opacity: ${opacity}` : ''}; | |
`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment