简体   繁体   English

如何为jQuery中的克隆元素设置动画

[英]How to animate cloned element in jQuery

Today I've been trying to write function which would clone an element, and then animate the clone, then delete previous one and loop it. 今天,我一直在尝试编写函数,该函数将克隆一个元素,然后对克隆进行动画处理,然后删除先前的一个并将其循环。

There is a badge with number on in, which counts how many times animation went off. 上面有一个带有数字的徽章,可以计算动画消失的次数。 I wanted to animate that badge, so it would scale 2x, get the next number and go to the normal size. 我想为该徽章制作动画,因此它将缩放2倍,获得下一个数字并恢复为正常大小。

Right now I'm trying to only animate the badge and then combine everything together. 现在,我仅尝试对徽章进行动画处理,然后将所有内容组合在一起。 I came up with that code: 我想出了那个代码:

function badgeClone() {

var badge1 = $('.badge');
var badge2 = $('.badge').clone();

var badgeWidth = badge1.width();
var badgeHeight = badge1.height();

    badge1.after(badge2);

    badge2.animate({
        'width': badgeWidth * 2 + 'px',
        'height': badgeHeight *2 + 'px'},
        500);

}

But to be honest, it does exactly nothing after calling that function in document.ready, there is no animation. 但老实说,在document.ready中调用该函数后,它什么也没做。 I assume I do something wrong, but logically, it should work. 我认为我做错了,但是从逻辑上讲,它应该可以工作。

I'd appreciate any help from you guys, cheers! 谢谢大家的帮助,加油!

EDIT: 编辑:

I added that counter to that function but I wonder why it does not return initial 0, it starts showing from "1". 我向该函数添加了该计数器,但是我不知道为什么它不返回初始0,而是从“ 1”开始显示。

var count = 0;

function badgeClone() {

var badge1 = $('.badge');
var badge2 = $('.badge').clone();

var badgeWidth = badge1.width();
var badgeHeight = badge1.height();

    badge1.text(count);

    badge1.after(badge2);

    badge2.animate({
        'width': badgeWidth * 2 + 'px',
        'height': badgeHeight *2 + 'px'},
        200, function() {

            count++
            badge2.text(count);

            badge2.delay(200).animate({
                'width': badgeWidth + 'px',
                'height': badgeHeight + 'px'},
                200);
            badge1.remove(); 
        });          
 }

That function is called later in other function, I'll try to make a similar fiddle with entire idea if necessary. 该函数稍后会在其他函数中调用,如有必要,我将尝试对整个想法进行类似的摆弄。

Cheers! 干杯!

Your code is okay. 您的代码还可以。 Im not sure how you call badgeClone(), but for me it works. 我不确定您如何调用badgeClone(),但对我来说却有效。 Have a look at this fiddle: http://jsfiddle.net/ 看看这个小提琴: http : //jsfiddle.net/

Please also look up the possible error in your console https://developers.google.com/chrome-developer-tools/docs/javascript-debugging?hl=de . 还请在您的控制台https://developers.google.com/chrome-developer-tools/docs/javascript-debugging?hl=de中查找可能的错误。

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

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