CSS トランジションで問題が発生しました。別のことを試す前に、何が問題なのかを理解したいと思います。
コンテナには 3 つのボックスと「次へ」ボタンがあります。目標は、次のボックスの上部が上部に表示され、「次へ」ボタンが押されたときにフェードインすることです。ボックスはコンテナに追加することで上に配置されるため、最後の要素として追加されて上に表示され、css トランジションによってフェードインする必要があります。
問題は、ボックスが追加された後に css トランジションが機能していないように見えることです。追加されていないボックス要素でテストすると、css トランジションはうまく機能します。
ここにフィドル、以下のコード:
HTML:
<div class="container">
<div class="box red"></div>
<div class="box blue"></div>
<div class="box green"></div>
</div>
<div id="next">Next</div>
JS:
var container = $(".container");
// Save the current list order
var list = container.children(".box");
// The current index
var current = 0;
// Put the first on top
container.append(list[0]);
function next() {
// Figure out what is the index of the next box
if (current + 1 < list.length) current++;
else current = 0;
// Save it in a variable
var target = $(list[current]);
// Put it on top
container.append(target);
// Hide it and then fade it in
target.css("opacity", 0).css("transition", "opacity 1000ms ease-out").css("opacity", 1);
// The fading in is not working
}
$("#next").click(next);
更新:
この問題の基本的な解決策は、不透明度を 0 に設定した後、トランジション css を設定する前に、ターゲットで offset() を呼び出すことでした。
target.css("opacity", 0);
target.offset();
target.css("transition", "opacity 1000ms ease-out").css("opacity", 1);