I want to make it so that elements with the class fade
fade in when they are first visible on the screen then fade out when they leave the screen.
I want this animation to only happen once.
Below is what I have tried.
.fade {
/* transition: opacity 0.9s ease-in;*/
opacity: 0;
}
.fade.visible {
transition: opacity 0.9s ease-in;
opacity: 1;
}
window.addEventListener('scroll', fade)
function fade()
{
let animation=document.querySelectorAll('.fade');
for (let i=0; i<animation.length; i++)
{
let windowheight=window.innerHeight;
let top=animation[i].getBoundingClientRect().top;
if (top < windowheight)
{
animation[i].classList.add('visible');
}
else
{
animation[i].classList.remove('visible');
}
}
}