Skip to content

Instantly share code, notes, and snippets.

@omar2205
Last active April 23, 2020 15:49
Show Gist options
  • Save omar2205/2de8d11d6af7c4c151a29a9df455562c to your computer and use it in GitHub Desktop.
Save omar2205/2de8d11d6af7c4c151a29a9df455562c to your computer and use it in GitHub Desktop.
js simple fade on scroll (Intersection Observer API)
const el = document.querySelector('.el')
let options = {
root: null, // avoiding 'root' or setting it to 'null' sets it to default value: viewport
rootMargin: '0px',
threshold: 0.5
}
let observer = new IntersectionObserver(([entry]) => {
if(entry.isIntersecting) {
el.classList.add('active')
} else {
el.classList.remove('active')
}
}, options)
observer.observe(el)
<!--...-->
<style>
h1.el {
opacity: 0;
transition: all 400ms ease;
transform: translateX(-100px);
margin-bottom: 50vh;
}
h1.el.active {
opacity: 1;
transform: translateX(0px);
}
</style>
<p>Scroll</p>
<div style="height: 300vh"></div>
<h1 class="el">Welcome</h1>
<!--...-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment