我遇到了 CSS 转换的问题,在尝试其他方法之前,我想了解问题所在。
一个容器中有 3 个框和一个“下一步”按钮。目标是让下一个框顶出现在顶部,并在按下“下一步”按钮时淡入。该框通过将其附加到容器来定位在顶部,因此它被添加为最后一个元素,因此在顶部可见,并且应该通过 css 过渡淡入。
问题是附加框后css转换似乎不起作用。如果在未附加的框元素上进行测试,则 css 转换效果很好。
Fiddle here,代码如下:
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);