Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery animations when appending html

I am trying to create the fadeIn effect when prepending a <UL> using Jquery, but the animation effect never appears on the page. Am i doing a mistake in specifying the fadeIn effect?

This is how my code looks

<UL id="myUL">
   <LI> First Item <LI>
   <LI> Second Item <LI>
   <LI> Third Item <LI>
   <LI> Fourth Item <LI>
<UL>

The Jquery is as follows (on button click)

var liData = "<LI> A New Item </LI>";
$('#myUL').prepend(liData).fadeIn('slow');

Though the <LI> appears correctly on the page, i donot see the fadeIn effect on the page. Am i doing something wrong in binding the data and the effect on the item?

like image 346
Abishek Avatar asked Aug 21 '11 20:08

Abishek


People also ask

Is jQuery slower for animations?

jQuery can be several times slower than CSS when talking about animations. CSS animations and transitions have the advantage of being hardware accelerated by the GPU, which is really good in moving pixels.

Which jQuery function is used to add animation to an element?

The . animate() method allows us to create animation effects on any numeric CSS property. The only required parameter is a plain object of CSS properties.

What is the use of the animate () method in jQuery?

The animate() method performs a custom animation of a set of CSS properties. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect. Only numeric values can be animated (like "margin:30px").


2 Answers

Added the display:none style, so Its not visible as soon as you prepend it and somewhat inverted the jQuery line.

var liData = '<LI style="display:none"> A New Item </LI>';
$(liData).prependTo('#myUL').fadeIn('slow');

JSFiddle : http://jsfiddle.net/sPeHy/4/

like image 77
IOrlandoni Avatar answered Oct 13 '22 00:10

IOrlandoni


Demo

This should do it

$('#myUL').prepend(liData).find("li:first").hide().fadeIn('slow');

Basically you were selecting the ul and not the li. Also you need to hide it before you fade it in or it will just fade from 100% to 100%, and you will see nothing in effect.

like image 33
Joseph Marikle Avatar answered Oct 12 '22 23:10

Joseph Marikle