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?
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.
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.
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").
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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With