I'm trying to animate some elements. There are spans inside a content-wrapper div.
Once we reach the div with the content-wrapper class, the b-fadein class is added to each span, and the animation should start, and the spans should appear one after another.
$(window).scroll(function() {
$('.el').each(function(){
var pos = $(this).offset().top;
var topOfWindow = $(window).scrollTop();
if (pos < topOfWindow+400) {
$(this).addClass("b-fadein");
}
});
});
Animation.
.b-fadein {
/* hidden by default */
opacity: 0;
animation: fadeIn 0.5s linear forwards;
animation-delay: calc(0.5 * var(--i));
}
@keyframes fadeIn {
from {
/* is not visible*/
opacity: 0;
}
to {
/* is visible */
opacity: 1;
}
}
HTML
<div class="content-wrapper" style="">
<!-- 0 -->
<span style="--i: 0; opacity:0;" class="el el1">1</span>
<!-- 1 -->
<span style="--i: 1; opacity:0;" class="el el2">2</span>
<!-- 2 -->
<span style="--i: 2; opacity:0;" class="el el3">3</span>
<!-- if more elements, increase the --i css variable for next elements -->
</div>
Currently, the desired effect is only visible when each span inside content-wrapper is displayed as a line (flex-direction is set to column).
But all spans can be on the same line.
The problem is that, if they are on the same line, they are displayed at the same time once we reach the content-wrapper.
How do we display the spans one after the other once we reach the content-wrapper div?