Search code examples
jquerycssjquery-animateprepend

Possible to animate jQuery prepend?


I am prepending some data to my page on a button click and instead of just populating immediately on the page, I was wondering if there is a way to animate the prepend() using slideToggle or CSS animation.

Here is my current script:

var data = $('.data').html();
var insert = '<div class="data-container">'+ data +'</div>';
$('button').click(function(){
    $('.data-container').remove();
    $('.initial').prepend(insert);
});

and a JSFiddle


Solution

  • You have to do something like this:

    var data = $('.data').html();
    var insert = '<div class="data-container">'+ data +'</div>';
    $('button').click(function(){
        $('.data-container').remove();
        $('.initial').hide();
        $('.initial').prepend(insert);
        $('.initial').slideToggle();
    
    });
    

    FIDDLE UPDATED