简体   繁体   English

使用jQuery更改HTML,但保留CSS3动画

[英]Change HTML with jQuery but preserve CSS3 Animations

I have a button that changes the HTML of a class called .content which has the following animation: 我有一个按钮可以更改名为.content的类的HTML,该类具有以下动画:

.content{
  -webkit-animation: fadein 2s;
  -moz-animation: fadein 2s;
}

With the click of a button I change its HTML like so: 通过单击按钮,我可以如下更改其HTML:

$('.button-about').click(function(){
    $('.content').html('<h1>Test</h1>');
});

This works fine, but the problem is that the animation isn't reloaded, the HTML is simply changed, which is what is expected. 这可以很好地工作,但是问题是动画没有被重新加载,HTML只是被更改了,这是所期望的。 Is there any way to preserve the animation when the HTML changes? HTML更改时,有什么方法可以保留动画吗?

Thanks! 谢谢!

EDIT: jsFiddle: http://jsfiddle.net/ar7wU/ 编辑:jsFiddle: http : //jsfiddle.net/ar7wU/

As Roko mentioned, changing HTML of an element will not trigger animations. 如Roko所述,更改元素的HTML不会触发动画。

However, you can achieve your desired outcome by: 但是,您可以通过以下方式实现所需的结果:

  1. Cloning your current element 克隆当前元素
  2. Replacing the HTML of the clone with your HTML 用您的HTML替换克隆的HTML
  3. Removing the existing element 删除现有元素
 var el = $('.content'), newone = el.clone(true); newone.html('<h1>Test</h1>'); el.before(newone); $(".content:last").remove(); 

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

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