Skip to content

Instantly share code, notes, and snippets.

@bedekelly
Last active September 26, 2019 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bedekelly/a365f83cd5e9f0d721840cc728c4fd95 to your computer and use it in GitHub Desktop.
Save bedekelly/a365f83cd5e9f0d721840cc728c4fd95 to your computer and use it in GitHub Desktop.
Quick'n'dirty parallax for a bunch of stacked layers
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