CSS3 transitionsを使用して、動的に作成された html 要素をアニメーション化しようとしています。
要素が作成される直前にアニメーションを開始したい。
これらのために、要素の元の位置を設定するクラスを作成し、jquery css()メソッドでターゲット位置を設定します
しかし、新しい要素は、遷移なしでターゲット位置に表示されます。
0ms の setTimeout を使用して新しい css 値を設定すると、機能します。
私が間違っていることがありますか?それとも制限ですか?setTimeout 回避策を使用する必要はないと思います。
ありがとう!
更新: 実験用に jsfiddle.net で実行されているコードへのリンクを次に示します。 http://jsfiddle.net/blackjid/s9zSH/
更新回答のソリューションで例を更新しました。
http://jsfiddle.net/s9zSH/52/
これは完全に機能するサンプルコードです
<html>
<head>
<script src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
//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");
});
</script>
<style type="text/css">
.animate{
-webkit-transition: all 1s ease-in;
}
.square{
width:50px;
height:50px;
background:red;
position:relative;
left:5px;
}
</style>
</head>
<body>
<div id="original">
<div class="square animate"></div>
</div>
<button class="duplicate timeout">duplicate with setTimeout</button>
<button class="duplicate notimeout">duplicate without setTimeout</button>
</body>
</html>