Last active
September 26, 2019 11:29
-
-
Save bedekelly/a365f83cd5e9f0d721840cc728c4fd95 to your computer and use it in GitHub Desktop.
Quick'n'dirty parallax for a bunch of stacked layers
This file contains 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
MAX_TRANSLATE = 6; | |
const elements = [ | |
"background", "clouds", "back-mountain", "front-mountain", | |
"hills-4", "hills-3", "hills-2", "hills-1", "birds" | |
].map(klass => document.querySelector(`.${klass}`)); | |
function updatePositions(height, width) { | |
const maxVHTranslation = -height * MAX_TRANSLATE; | |
const maxVWTranslation = -width * MAX_TRANSLATE; | |
const length = elements.length; | |
for (let i=0; i<elements.length; i++) { | |
const translateVH = i / length * maxVHTranslation; | |
const translateVW = i / length * maxVWTranslation; | |
elements[i].style.transform = | |
`translate3d(${translateVW}vh, ${translateVH}vh, 0)`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment