Last active
August 24, 2022 11:47
-
-
Save ryandejaegher/a42fa66b2ab70b41f901d2c802fd43c9 to your computer and use it in GitHub Desktop.
Add ability to add parallax for individual sections
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 onScroll(e) { | |
| console.log(e); | |
| console.log('yes'); | |
| } | |
| var observer = new IntersectionObserver(entries => { | |
| console.log(entries) | |
| entries.forEach(entry => { | |
| console.log(entry) | |
| if (entry.isIntersecting === true && entry.intersectionRatio > 0) { | |
| entry.target.style.transform = translateY(entry.intersectionRatio, '20%') | |
| } else { | |
| document.removeEventListener('scroll', onScroll, {capture:false,passive:true}); | |
| } | |
| }); | |
| }, {threshold: buildThresholdList()}); | |
| var sections = document.querySelectorAll('section'); | |
| sections.forEach(item => { | |
| observer.observe(item) | |
| }); | |
| function buildThresholdList() { | |
| let thresholds = []; | |
| let numSteps = 100; | |
| for (let i=1.0; i<=numSteps; i++) { | |
| let ratio = i/numSteps; | |
| thresholds.push(ratio); | |
| } | |
| thresholds.push(0); | |
| return thresholds; | |
| } | |
| function translateY(ratio, total) { | |
| return `translateY(calc(-${ratio} * ${total})`; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment