I'm trying to animate a div that I previously appended, but the animation doesn't happen, even though the complete
function is executed.
Here is a very simple example:
EDIT: I corrected the typo in my jsFiddle and the problem is still there, but thank you for pointing it out.
HTML:
<button id="test">
Go
</button>
<div class="elements">
</div>
JS:
$('#test').click(function(){
$("<div class='element-test'><img src='https://image.flaticon.com/icons/png/128/148/148836.png' style='position:absolute; z-index:40; top:400px; left:200px; width:2%;'></div>")
.appendTo('.elements').animate({top:'100px', left:'400px'}, 500, function(){
$('.element-test').fadeOut(200);
});
});
I wrote a jsFiddle showing the problem.
In my application, I would need to animate this with % positions, and I thought that was the problem in the beginning. However, changing it to pixel positions or even pure int numbers in the animate
parameters doesn't solve it.
At the beginning, I was using .append()
and then animate()
. The idea of using appendTo().animate()
comes from there.