What I would like to do (broke):
<div></div>
<button>go</button>
$('button').click(function () {
$('div').css({
'transition': 'left 1000ms'
}).addClass('left').addClass('left_more');
});
http://jsfiddle.net/0bm4wq7h/13/
Still broke:
<div></div>
<button>go</button>
$('button').click(function () {
$('div').css({
'transition': 'left 1000ms'
}).addClass('left');
console.log('test');
$('div').addClass('left_more');
});
http://jsfiddle.net/fwL3dwz2/3/
But this works:
<div></div>
<button>go</button>
$('button').click(function () {
$('div').css({
'transition': 'left 1000ms'
}).addClass('left');
console.log($('div').css('left'));
$('div').addClass('left_more');
});
http://jsfiddle.net/j8x0dzbz/5/
I know I need a starting point for my CSS transition. That is why I added the left class.
Why does jQuery not do the transition until my #3?
Update:
So I had accepted Stryner's answer, because it was working for me and now I'm having the same issue again. The above code was a simplified version of this JavaScript:
$('#screen_wrapper img:eq(0)').removeClass().addClass('prep'); //starting point
window.getComputedStyle(document.getElementById('photo'+0)).left; //force the the styling to get recomputated using raw JavaScript
$('#screen_wrapper img:eq(0)').css('left');//force the the styling to get recomputated using jQuery
$('#screen_wrapper img:eq(0)').addClass('animate_in');//animate to the "animate_in" class coordinates from the "prep" class coordinates
What's happening is that I'm getting the animation starting from coordinates prior to the prep class.
Here's the prep class:
#screen_wrapper img.prep {
top: 0px;
left: 506px;
}
But the image is actually starting from this class which is removed using the removeClass() jQuery method:
.animate_out {
visibility: visible;
top: 0px;
left: -506px;
}
The transition property is working properly:
$('#screen_wrapper img').css('transition','left 1000ms');
I have doubts that these force recalculation of styling:
window.getComputedStyle(document.getElementById('photo'+0)).left;
$('#screen_wrapper img:eq(0)').css('left');
I'm using Chromium:
Version 45.0.2454.101 Ubuntu 14.04 (64-bit)
Update
Example of it not working: http://jsfiddle.net/me8ukkLe/12/
transition. When you force the recalculation, it starts transitioning to the value of the new class, but since you're instantly adding a new class after, that transition is getting interrupted. The reason this didn't occur in your original example is that the<div>had no initialleftvalue to transition from. If you add it, it does the same thing. Also see jsfiddle.net/me8ukkLe/13