简体   繁体   English

当我删除新创建的元素类时,CSS3过渡不起作用

[英]CSS3 transition doesn't work when I remove class of newly created element

What I do: 我所做的:

  1. Create an element, which style contains a CSS3-transition. 创建一个元素,该样式包含CSS3转换。
  2. Add a class to it (style for this class contains background-color property). 向其添加一个类(此类的样式包含background-color属性)。
  3. Add an innerHTML to it. 添加innerHTML。
  4. Remove the class. 删除课程。

When I remove the class just like 当我删除课程时就像

element.classList.remove('classname')

there are no transition, but when I set a timeout with 0s delay, it works: 没有转换,但是当我设置0s延迟超时时,它可以工作:

setTimeout(function() { element.classList.remove('classname'); }, 0);

Why? 为什么? How can I avoid setTimeout function or is it normal to use it? 如何避免使用setTimeout函数或使用它是否正常?

Example: http://jsfiddle.net/JHevJ/1/ 示例: http//jsfiddle.net/JHevJ/1/

Because you're removing the class before the screen calculates the redrawing of elements. 因为您在屏幕计算重新绘制元素之前删除了类。 You're removing it before it's shown as that class in the first place, so it doesn't think an animation is necessary. 你在它首先显示为该类之前将其删除,因此它不认为动画是必要的。

The timeout makes it so it "waits" 0 milliseconds and then removes the class, which lets it show the element with the class first, "wait" 0 time and then remove the class. 超时使得它“等待”0毫秒然后删除该类,这使得它首先显示具有类的元素,“等待”0时间然后删除该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM