I don't know how many people have wrote this kind of articles, but wrote fade in image or text without jQuery.
.fadein {
opacity: 0;
}
.fadein.is-active {
opacity: 1;
transition: all ease 0.65s;
}
// Fade in Threshold
const screenOffset = window.innerHeight / 2
const elements = document.getElementsByClassName('fadein')
for (let element of elements) {
window.addEventListener('scroll', () => {
if (window.scrollY + screenOffset > element.offsetTop) {
element.classList.add('is-active')
}
})
}
<p style="height: 100vh">Prints out this usage information.</p>
<img class="fadein" src="https://www.google.co.jp/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
If you use React, you should write the above code in componentDidMount
.
Ref:
Top comments (3)
You should use data attributes to show Ui state changes
Thank you for your comment.
Did you mean attribute like
<img src="..." data-active />
?I will try it though I actually do not know the advantage.
Because you can make it readable by accesibility technologies