Note that there are some explanatory texts on larger screens.

plurals
  1. POCSS3 transitions to dynamically created elements
    primarykey
    data
    text
    <p>I'm trying to <strong>animate</strong> a dynamically created html element with <strong>CSS3 transitions</strong>. </p> <p>I want the animation to start just before the element is created.<br> For these i create a class that set the original position of the element and then I set the target position by the jquery <em>css()</em> method </p> <p>But the new element it just apears in the target position without any transition.</p> <p>If I use a setTimeout of 0ms to set the new css value it work.</p> <p>There is something I'm doing wrong? or is a limitation? I don't think I should need to use the setTimeout workaround.</p> <p>Thanks!</p> <p><strong>UPDATE</strong>: Here is a link with the code running on jsfiddle.net for you to experiment. <a href="http://jsfiddle.net/blackjid/s9zSH/" rel="noreferrer">http://jsfiddle.net/blackjid/s9zSH/</a></p> <p><strong>UPDATE</strong> I've updated the example with the solution in the answer.<br> <a href="http://jsfiddle.net/s9zSH/52/" rel="noreferrer">http://jsfiddle.net/s9zSH/52/</a></p> <p>Here is a fully working example code</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script src="http://code.jquery.com/jquery-1.4.3.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; //Bind click event to the button to duplicate the element $(".duplicate").live("click", function (e){ var $to = $("#original .square").clone() $("body").append($to); if($(e.target).hasClass("timeout")) //With setTimeout it work setTimeout(function() { $to.css("left", 200 + "px"); },0); else if($(e.target).hasClass("notimeout")) // These way it doesn't work $to.css("left", 200 + "px"); }); &lt;/script&gt; &lt;style type="text/css"&gt; .animate{ -webkit-transition: all 1s ease-in; } .square{ width:50px; height:50px; background:red; position:relative; left:5px; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="original"&gt; &lt;div class="square animate"&gt;&lt;/div&gt; &lt;/div&gt; &lt;button class="duplicate timeout"&gt;duplicate with setTimeout&lt;/button&gt; &lt;button class="duplicate notimeout"&gt;duplicate without setTimeout&lt;/button&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload