Search code examples
jqueryanimationclone

Why can't I animate a cloned element?


This is my code :

var newElement=$('.oggetto').eq(0).clone();
newElement.animate({ 'top': '2000px'}, 5000);

<div id="container">
    <div class="oggetto" style="left:0px;">&nbsp;</div>
    <div class="oggetto" style="left:50px;">&nbsp;</div>
</div> 

but seems that ".oggetto" won't to move after the clone().

In fact, if I just write :

$('.oggetto').eq(0).animate({ 'top': '2000px'}, 5000);

it works as well. Where am I wrong?


Solution

  • Because, first the cloned element have to inserted into the DOM and then should be animated.

    var newElement=$('.oggetto').eq(0).clone();
    $("#container").append(newElement); //add the element 
    
    //Now animate
    newElement.animate({ 'top': '2000px'}, 5000);
    

    Demo