开发者

Apply animations on Clone and Remove functions

I have clone and remove functions on my page, to which I want to apply slideDown / slideUp animations to respectively:

$('#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;
});

I have tried doing this but either it doesn't work or 开发者_运维问答the animation is very jerky. Anyone know how this can be done?


for the cloning

$('#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;
});

and for the removing

$('.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;
});

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜