Even though it's already been provided in comments to the other answers, here is a pretty complete list of triggers. Note that for getters like elem.offsetWidth
, there is no need to wrap it in any call. The getter will be called anyway, and if a compiler were to optimize this out, it would be terribly broken.
Triggering a reflow is sub-optimal and can be very harmful if misused.
To understand what is a reflow and why it's needed in this case, I invite you to read this answer of mine and the associated ones. TL;DR the browser will try to wait until the last minute before recalculating the whole CSSOM boxes and styles of the page and will thus only see the last status where the transition is applied, discarding the fact that it has been removed temporarily. Requesting a reflow synchronously will force the CSSOM to recalculate all the boxes and styles in the page and it will see the state where the transition wasn't set, and thus it will see that when it's added back it should perform a transition.
Now, performing a reflow means that the engine has to recalculate all the boxes of all the elements in the document. That can really computationally heavy, and if used in a loop, or in a very complex DOM structure, it can make your whole page lag out terribly.
It didn't exist in 2014, but it is supported in all modern browsers for years now. The Web Animations API allows us to tap directly in the animation engine where the CSSOM will move its own animations and transitions.
Thanks to it, we don't need to go the slow and complex path of DOM manipulation -> CSSOM recalc -> animation engine.
You don't need to worry about style recalcs, and the API is JS friendly, returning clear Promises you can await on instead of dealing with the multitude of CSS animation-x
events.
To reproduce OP's fiddle with the Web Animations API, you can simply do:
function makeAnimation()
{
const ul = document.getElementsByTagName('ul')[0];
ul.animate(
[ // The "keyframes" of our animation/transition
{ left: "-600px", },
{ left: "-0px", }
],
{ duration: 200/* ms */, easing: "ease-out" }
);
}
.viewport { width: 600px; height: 300px; }
ul { list-style: none; margin: 0; padding: 0; position: relative; width: 250%; }
ul li { display: block; float: left; width: 600px; height: 300px; line-height: 300px; font-size: 30px; text-align: center; }
<div class="viewport">
<ul>
<li style="background: lightblue; color: red">1</li>
<li style="background:gray; color: black;">2</li>
</ul>
</div>
<button onclick="makeAnimation()">Make animation</button>
What if I really need to trigger a reflow?
That should probably be its own Q/A, but in such a case, the best is to batch all your reflow triggers in a single place and make sure nothing will dirty the box model. This is far from being easy, as it requires you have full control over what modifies the DOM. But the basic idea is that in whatever stage of the Event-Loop your code runs, you make all your DOM modifications, without calling a single trigger (beware some are sneaky).
Then you wait until the next ResizeObserver callback. Indeed, these callbacks are called after the browser did its own recalc (specs, step 16). So this means that if we do wait until there, and then call elem.offsetWidth
, the browser will already have the given information cached and won't need to perform a new recalc & reflow. However for this to stand, you must not dirty the DOM during that phase. One strategy I use is thus to have a two phases handler in the ResizeObserver callbacks where I execute a first batch of callbacks that will gather and return all the computed values needed, and then another batch of callbacks that will perform the DOM manips based on the computed values.
This strategy allows me to fire most of the time only 2 reflows per frame (the one from the browser, and the one for my scripts). But even when you know very well what triggers a reflow, you can always discover new sneaky ones, and there are cases like scrollTo()
which will both trigger a reflow and dirty the boxes for which you can't do anything...
none 0s linear
nothing will animate until you change theleft
property. – Manufacturer