Skip to content

Instantly share code, notes, and snippets.

@ryandejaegher
Last active August 24, 2022 11:47
Show Gist options
  • Select an option

  • Save ryandejaegher/a42fa66b2ab70b41f901d2c802fd43c9 to your computer and use it in GitHub Desktop.

Select an option

Save ryandejaegher/a42fa66b2ab70b41f901d2c802fd43c9 to your computer and use it in GitHub Desktop.
Add ability to add parallax for individual sections
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