addClass()是异步的吗?

时间:2016-12-02 14:00:43

标签: jquery css transition

它应该删除类transition(因此CSS转换属性),将div移动到200px(立即),重新应用transition css属性而不是动画(花了1秒)div到右边。相反,它冻结了。

看起来应用css left属性需要花费更多时间按类删除transition?或者是addClass()异步?

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>

2 个答案:

答案 0 :(得分:5)

不,addClass是同步的。您可以查看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;
}

如果使用本机API而不是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>

当您更改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>

强制使用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>

答案 1 :(得分:0)

以下是我的警告声明,用于以奇怪的方式显示事件。在这里,您可以清楚地看到向左移位: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>