I have a text "HELLO" and I want to loop through every letter and animate it so it fades in and out.Here's my code.
EDIT: I put the answer in the snippet to see it in action.
Code:
$(document).ready(function() {
var $letters = $('p[id^="letter-"');
$letters.each(function(index) {
$(this).css({
'animation': 'pulse 500ms ' + index * 500 + 'ms' + ' linear'
})
});
});
html,
body {
font-size: 45px;
}
p {
position: absolute;
left: 400px;
top: 100px;
color: rgba(0, 0, 0, 0);
}
@keyframes pulse {
0% {
color: rgba(0, 0, 0, 0);
}
25% {
color: rgba(0, 0, 0, 0.5);
}
50% {
color: rgba(0, 0, 0, 1);
}
75% {
color: rgba(0, 0, 0, 0.5);
}
100% {
color: rgba(0, 0, 0, 0);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id='letter-0'>H</p>
<p id='letter-1'>E</p>
<p id='letter-2'>L</p>
<p id='letter-3'>L</p>
<p id='letter-4'>O</p>
And here's a link to a pen. Instead of doing the animation one letter at a time, it's animating the whole thing at once.How can this be fixed? Shouldn't a loop finish executing all the commands and then move on to the next step? Maybe there's a better approach to this that I don't know of?