It should remove class transition
(thus CSS transition property), move the div at 200px (immediately), re-apply the transition
css property and than animate (taking 1 second) the div to the right. Instead, its frozen.
It looks like apply the css left
property takes more time of removing transition
by class? Or is addClass()
async?
var elem = $('#elem');
elem.removeClass('transition');
elem.css('left', 200);
elem.addClass('transition');
elem.css('left', 0);
#container {
position: relative;
width: 400px;
height: 200px;
background-color: red;
}
#elem {
width: 50px;
height: 50px;
position: relative;
left: 0;
top: 0;
background-color: blue;
}
.transition.linear.scritta {
-webkit-transition: all 1.0s linear;
-moz-transition: all 1.0s linear;
-ms-transition: all 1.0s linear;
-o-transition: all 1.0s linear;
transition: all 1.0s linear;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="elem" class="transition linear scritta"></div>
</div>
No, addClass
is synchronous. You can look at the source code:
addClass: function( value ) {
var classes, elem, cur, curValue, clazz, j, finalValue,
i = 0;
if ( jQuery.isFunction( value ) ) {
return this.each( function( j ) {
jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
} );
}
if ( typeof value === "string" && value ) {
classes = value.match( rnothtmlwhite ) || [];
while ( ( elem = this[ i++ ] ) ) {
curValue = getClass( elem );
cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
if ( cur ) {
j = 0;
while ( ( clazz = classes[ j++ ] ) ) {
if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
cur += clazz + " ";
}
}
// Only assign if different to avoid unneeded rendering.
finalValue = stripAndCollapse( cur );
if ( curValue !== finalValue ) {
elem.setAttribute( "class", finalValue );
}
}
}
}
return this;
}
You would see the same result if you used native APIs instead of jQuery.
var elem = document.getElementById('elem');
elem.classList.remove('transition');
elem.style.left = '200px';
elem.classList.add('transition');
elem.style.left = '0px';
#container {
position: relative;
width: 400px;
height: 200px;
background-color: red;
}
#elem {
width: 50px;
height: 50px;
position: relative;
left: 0;
top: 0;
background-color: blue;
}
.transition.linear.scritta {
-webkit-transition: all 1.0s linear;
-moz-transition: all 1.0s linear;
-ms-transition: all 1.0s linear;
-o-transition: all 1.0s linear;
transition: all 1.0s linear;
}
<div id="container">
<div id="elem" class="transition linear scritta"></div>
</div>
It seems that browsers don't directly update the document when you change a CSS style. That could produce paintings and relayouts and expensive operations, which would be useless if you are going to change more styles.
So they wait a bit to let you change more styles, and then they update them all simultaneously.
As a solution, you can try using asynchroneous code:
requestAnimationFrame(function() {
elem.style.left = '0px';
});
var elem = document.getElementById('elem');
elem.classList.remove('transition');
elem.style.left = '200px';
elem.classList.add('transition');
requestAnimationFrame(function() {
elem.style.left = '0px';
});
#container {
position: relative;
width: 400px;
height: 200px;
background-color: red;
}
#elem {
width: 50px;
height: 50px;
position: relative;
left: 0;
top: 0;
background-color: blue;
}
.transition.linear.scritta {
-webkit-transition: all 1.0s linear;
-moz-transition: all 1.0s linear;
-ms-transition: all 1.0s linear;
-o-transition: all 1.0s linear;
transition: all 1.0s linear;
}
<div id="container">
<div id="elem" class="transition linear scritta"></div>
</div>
Force an update with getComputedStyle
seems to work too.
getComputedStyle(elem).transitionDuration; // force update
var elem = document.getElementById('elem');
elem.classList.remove('transition');
elem.style.left = '200px';
elem.classList.add('transition');
getComputedStyle(elem).transitionDuration; // force update
elem.style.left = '0px';
#container {
position: relative;
width: 400px;
height: 200px;
background-color: red;
}
#elem {
width: 50px;
height: 50px;
position: relative;
left: 0;
top: 0;
background-color: blue;
}
.transition.linear.scritta {
-webkit-transition: all 1.0s linear;
-moz-transition: all 1.0s linear;
-ms-transition: all 1.0s linear;
-o-transition: all 1.0s linear;
transition: all 1.0s linear;
}
<div id="container">
<div id="elem" class="transition linear scritta"></div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With