Picture this scenario with 3 div elements:
<div class="one animated">
I represent the first div
</div>
<div class="two">
second div
</div>
<div class="three">
Last one
</div>
There is some code to move only the first div to the top:
.animated {
animation: animation 2s;
animation-fill-mode: forwards;
}
@keyframes animation {
0% { top: 0; opacity: 1 }
100% { top: -300px; opacity: 0 }
}
The question arises as to why the first div moves to the top while the others do not. Since the first div disappears, the solution is to replace it with the other two divs...
This can be achieved by doing something like the following:
function test() {
document.getElementById("sample").classList.add("animated");
}
.one {
border: 1px solid red;
}
.animated {
animation: animation 2s;
animation-fill-mode: forwards;
}
@keyframes animation {
0% { top: 0; opacity: 1 }
100% { top: -300px; opacity: 0 }
}
<div class="one" id="sample">
I'm the first div
</div>
<div class="two">
second div
</div>
<div class="three">
Last one
</div>
<button onclick="test()">Test</button>