简体   繁体   English

重新启动/重置并重播过渡CSS?

[英]restart/reset and replay a transition css?

I need to replay a css transition using javascript. 我需要使用javascript重播CSS转换。 When I reset css style of my div and apply the new transition, nothing happens... 当我重设div的CSS样式并应用新的过渡时,什么也没有发生...

I think both code are executed in the same execution frame and by optimisation, it doesn't change anything... 我认为这两个代码都在同一执行框架中执行,并且经过优化,它不会改变任何内容...

Example : https://jsfiddle.net/f0s8a8jp/2/ 范例: https : //jsfiddle.net/f0s8a8jp/2/

test.css({transition: 'none', height: '0px'});
test.css({transition: 'height 1s linear', height: '100px'});

The only solution I found for the moment (not realy clean for me), is to use setTimeout beetween the properties reset and the application of new transition : https://jsfiddle.net/f0s8a8jp/3/ 我目前发现的唯一解决方案(对我来说不是很干净),是在属性重置和新过渡的应用之间使用setTimeout: https : //jsfiddle.net/f0s8a8jp/3/

Better idea is welcome?! 欢迎更好的主意?!

The transition will apply on the computed dimension of the element. 过渡将应用于元素的计算尺寸。 Setting the CSS property does not compute it. 设置CSS属性不会对其进行计算。 So, setting forth and back the property is like letting it unchanged. 因此,来回设置属性就像让它不变。 It's computed when the browser need to reflow the element. 它是在浏览器需要重排元素时计算的。

setTimeout will eventually let the time to the browser to compute a reflow in the meanwhile. setTimeout最终会将时间setTimeout浏览器,同时计算回流。 You could use requestAnimationFrame to achieve the same without flickering. 您可以使用requestAnimationFrame实现相同效果而不会闪烁。

Another hack consists in forcing the browser to reflow right after altering the CSS properties, using test[0].offsetWidth; 另一test[0].offsetWidth;在更改CSS属性后使用test[0].offsetWidth;强制浏览test[0].offsetWidth; for instance. 例如。 The transition is then performed correctly: https://jsfiddle.net/f0s8a8jp/9/ 然后可以正确执行转换: https : //jsfiddle.net/f0s8a8jp/9/

A more in-depth discussion about reflow (and repaint) can be found here: http://www.phpied.com/rendering-repaint-reflowrelayout-restyle/ 可以在以下位置找到有关回流(和重绘)的更深入的讨论: http : //www.phpied.com/rendering-repaint-reflowrelayout-restyle/

Try this: 尝试这个:

https://jsfiddle.net/f0s8a8jp/4/ https://jsfiddle.net/f0s8a8jp/4/

I made it work on click , so you need to click on it to see the result. 我让它在click起作用,因此您需要单击它才能查看结果。

The only thing I really changed was the order of the code. 我真正改变的唯一一件事就是代码的顺序。

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

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