The Problem
I want to position an element using javascript and then add a transition-appliance class to it, so that after positioning, any further changes will be animated by that transition.
Generally that is not a big deal.
But in order to achieve it, i find myself writing code like this:
$('#someWindow').center('#someParent');
$('#someWindow').addClass('transition visible');
So what's the catch?
Both calls are (as i understand it) rendered by the browser within one processing step. And that results in either no transition at all, or in all changes to the element being animated by the transition - including those that were applied before the transition
-class was applied to the element.
Solution?
My first thought was to wrap the second call in a setTimeout()
call.
$('#someWindow').center('#someParent');
window.setTimeout(function(){
$('#someWindow').addClass('transition visible');
},1);
It works! Using setTimeout()
the rendering is now somehow stacked and the browser does it all correctly.
So what's the question?
I assume this is a known problem. My solution doesn't satisfy me. It 'feels' inconsistent, as if i could not fully rely on it.
So, is this the only way to do it? Or are there any more clean ways to work around that problem?
UPDATE
See this fiddle for a demonstration
(Remember to press the reset button in between testing).