I have a growing list of divs. The user can click a button to append a new div to the bottom.
This list has a fade-in animation, this is so that the user can see when the list has been loaded (it gets data from a server).
When a new row gets added, I’d like to not have the fade-in animation.
I tried using:
div:last-child {
animation: none;
}
This works only the first time a new div has been added. On all the following additions, the animation gets applied regardless.
Please check this demo below to see what I mean:
function addRow(text) {
var d = document.createElement('div');
d.innerHTML = text;
var root = document.querySelector('main');
root.appendChild(d);
}
body {
background: #222;
}
main > div {
background: #444;
border: 1px solid green;
animation: fade-in 1s;
}
div:last-child {
background: #999;
animation: none;
}
@keyframes fade-in {
from { opacity: 0 }
to { opacity: 1 }
}
<main>
<div>1</div>
<div>2</div>
<div>3</div>
</main>
<button type="button" onclick="addRow('hello')">Add row</button>
How do I ensure the animation runs on the whole list the first time, but prevent it on subsequent additions?