Is addClass () async?

It should remove the class transition(thus the CSS transition property), move the div to 200px ( immediately ), reapply the transitioncss property and than wait (take 1 second) the div to the right. Instead, its frozen.

Looks like applying a property css lefttakes longer to delete transitionby class? Or 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>
Run codeHide result
+4
source share
2 answers

No, it addClassis synchronous. You can see 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 will see the same result if you use your own 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>
Run codeHide result

, CSS. , , .

, , , .

:

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>
Hide result

getComputedStyle, , .

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>
Hide result
+4

, , . : 200 , , elem.css('left', 200); .

var elem = $('#elem');
elem.removeClass('transition');
alert('1');
elem.css('left', 200);
elem.addClass('transition');
alert('2');
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>
Hide result
0

Source: https://habr.com/ru/post/1662647/


All Articles