I ran into a CSS transition problem, and before trying anything else, I want to understand what the problem is.
The container has 3 boxes and a "Next" button. The goal is for the next top of the window to appear on top and disappear when the "Next" button is clicked. The box is positioned at the top, adding it to the container, so that it is added as the last element and, thus, is displayed at the top, and should disappear through the css transition.
The problem is that the css transition does not work after adding the field. The css transition works well if checked for a box item that is not added.
The script is here , the code is below:
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);
Update
The main solution to this problem was to call offset () on the target after setting the opacity to 0 and before setting the css transition:
target.css("opacity", 0); target.offset(); target.css("transition", "opacity 1000ms ease-out").css("opacity", 1);
Updated version of the above scripts
source share