I'm a little bit stuck on animations, so I have a div that is added dynamically using jQuery .append()
so when the page is loaded the div content will be added and from css I'll apply some animations using @keyframes
. My problem is when the div content is closed using jQuery .remove()
because if the div content is removed how I will apply animation to this ? So basically on page loaded the content will animate from top to bottom and on close should go back from bottom to top, how can I do that reverse animation ? I want to apply that reverse animation using only css not js.
.child-container {
background: red;
height: 100px;
width: 150px;
padding: 10px;
animation-name: anime;
animation-duration: 1s;
}
@keyframes anime {
from,
0%,
100%,
to {
animation-timing-function: cubic-bezier(0.25, 0.60, 0.35, 1.00);
}
0% {
opacity: 0;
transform: translate3d(0, -200px, 0);
}
100% {
opacity: 1;
transform: translate3d(0, 0, 0);
}
}
$(document).ready(function() {
$(".container").append($("<div class='child-container'>Hello Jimmy!<a class='close'><b>X</b></div>"));
$(".close").on('click', function() {
$(this).parent().remove();
});
});
.container {
padding: 10px;
background: orange;
}
.child-container {
background: red;
height: 100px;
width: 150px;
padding: 10px;
animation-name: anime;
animation-duration: 1s;
}
.close {
float: right;
cursor: pointer;
}
.close:hover {
color: #fff;
}
/*ANIMATIONS*/
@keyframes anime {
from,
0%,
100%,
to {
animation-timing-function: cubic-bezier(0.25, 0.60, 0.35, 1.00);
}
0% {
opacity: 0;
transform: translate3d(0, -200px, 0);
}
100% {
opacity: 1;
transform: translate3d(0, 0, 0);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container"></div>