Created
April 11, 2019 19:35
-
-
Save SketchBookkeeper/97006dcc74f452f49636979f2fabdc99 to your computer and use it in GitHub Desktop.
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
| // .js-parallax element should have a css bg image. | |
| const parallaxItems = Array.from(document.querySelectorAll('.js-parallax')); | |
| function runParallax() { | |
| parallaxItems.forEach(item => { | |
| const rect = item.getBoundingClientRect(); | |
| const offset = ((rect.top / rect.height) * 100) * 0.25; | |
| item.style.backgroundPosition = `50% ${offset}%` | |
| }); | |
| } | |
| runParallax(); | |
| document.addEventListener('scroll', function() { | |
| runParallax(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment