option css3,jquery - Css3 animation reset option - Stack Overflow

重启动画技巧
本文介绍了一种通过克隆、移除原有元素并追加新元素来重新启动CSS动画的方法。此方法适用于jQuery及纯JavaScript实现,并提供了具体的代码示例。同时,文章还探讨了使用动画延迟属性和定时器来重启动画的可能性。

As @Fabrizio Calderan pointed out, you'll have to clone, remove and append a new element to restart the animation.

Just removing and adding the class won't work because we will restart an animation without telling that the animation is stopped so the listener won't be activated...

So to solve your problem you should recreate and append a new element like this (this is the jQuery way to stay simple, but you can do it with vanilla JS too) :

function reanimateWrapper(){

var $wrapper = $('#animation-bg');

var $clonedWrapper = $wrapper.clone().removeClass('animate');

$wrapper.remove();

$('body').prepend($clonedWrapper);

$('#animation-bg').addClass('animate');

}

And as a CSS tips, the animation-delay property can be specified in the generic animation property too. Like :

animation: single-animation-name animation-duration animation-direction animation-delay

See full solution with this updated fiddle : https://jsfiddle.net/75qb2sfk/3/

Note: I added jQuery to the fiddle to keep the code simple, and restructured your CSS to have a specific class for animations)

Update: after reading the CSStrick link of Fabrizio, it seems that there is a way to restart the animation by removing then adding the class if we use a timer. But as they wrote in the article, this isn't a good way to go...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值