Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 617
APAD1 Avatar asked Jun 26 '14 15:06

APAD1


People also ask

What can you animate with jQuery?

jQuery animate() Method The animate() method is typically used to animate numeric CSS properties, for example, width , height , margin , padding , opacity , top , left , etc. but the non-numeric properties such as color or background-color cannot be animated using the basic jQuery functionality.

What is the correct syntax to call the jQuery animate () function?

The animate() is an inbuilt method in jQuery which is used to change the state of the element with CSS style. This method can also be used to change the CSS property to create the animated effect for the selected element. Syntax: (selector).

Which jQuery method is used to stop an animation before it is finished?

The jQuery stop() method is used to stop an animation or effect before it is finished. The stop() method works for all jQuery effect functions, including sliding, fading and custom animations. Syntax: $(selector).

Which jQuery method can be used to gradually change the height of the selected element?

jQuery height() Method The height() method sets or returns the height of the selected elements.


1 Answers

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

like image 145
Ehsan Sajjad Avatar answered Oct 27 '22 15:10

Ehsan Sajjad