在克隆和删除功能上应用动画

时间:2011-01-11 22:28:55

标签: jquery jquery-animate

我在页面上有克隆和删除功能,我想分别应用slideDown / slideUp动画:

$('#add-item').click(function(){
    var divCloned = $('.form-fields:first').clone();
    divCloned.insertAfter('.form-fields:last');
    return false;
});

$('.remove').live('click', function(){
    if(confirm("Are you sure you wish to remove this item?"))
    {
        $(this).parent().remove();
    }
    return false;
});

我试过这样做,但要么它不起作用,要么动画非常生涩。任何人都知道如何做到这一点?

1 个答案:

答案 0 :(得分:5)

用于克隆

$('#add-item').click(function(){
    var divCloned = $('.form-fields:first').clone();
    // first we hide it (set display to none), then we add it in the DOM 
    // and lastly we aninmate it with slideDown
    divCloned.hide().insertAfter('.form-fields:last').slideDown('fast');
    return false;
});

和删除

$('.remove').live('click', function(){
    if(confirm("Are you sure you wish to remove this item?"))
    {
        // we use the callback method of the slideUp function
        // so that the removal happens after the animation..
        $(this).parent().slideUp('fast',function(){ $(this).remove(); });
    }
    return false;
});

http://www.jsfiddle.net/gaby/qf4j3/

演示