Last active
April 23, 2020 15:49
-
-
Save omar2205/2de8d11d6af7c4c151a29a9df455562c to your computer and use it in GitHub Desktop.
js simple fade on scroll (Intersection Observer API)
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
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) |
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
<!--...--> | |
<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