Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: Append Div and html slowly with effect

Tags:

jquery

i am fetching data using ajax.

  <ul class="products">

  <li>Row1</li>

  <li>Row1</li>

  <li>Row1</li>

  <li>Row1</li>

  </ul>

when user click on li the li will append.

   jQuery(function(){

  jQuery('li').click(function(){

  jQuery('.products').append('<li class="new-rows"></li>');

  jQuery('.new-rows').html(dd , 500);

  });

 });

now what i am looking for is new generated li display slowly.

here dd is the content getting from another page using ajax;

check this fiddle : http://jsfiddle.net/adHvb/2/

like image 814
Jack Torris Avatar asked Nov 29 '13 06:11

Jack Torris


2 Answers

Try this out:- http://jsfiddle.net/adiioo7/adHvb/6/

JS:-

jQuery('li').click(function () {
      dd = 'baba';
      var liData = '<li class="new-rows" style="display:none;"></li>';
      $(liData).appendTo('.products').fadeIn('slow');

      jQuery('.new-rows').html(dd, 500);
  });
like image 139
Aditya Singh Avatar answered Nov 24 '22 18:11

Aditya Singh


All the answers here regarding animation and effects are really good but I am mostly concern about the fetching part since you didn't include that. Do you fetch the data on the start(document ready) or only when <li> is clicked?

If you fetch the data when the <li> click() event is fired you cannot just set a specific delay for the animation. What if it took some time to fetch the data?

I think you should do the appending and animation on jQuery.ajax() success function.

jQuery('li').click(function(){
    var $newRow = jQuery('<li class="new-rows"></li>');

    jQuery.ajax({
        type: 'type',
        url: 'url',
        data: data,
        success: function(dd){
            $newRow.html(dd).appendTo('.products').hide().fadeIn(1000);     
        }
    });

});
like image 26
Mark S Avatar answered Nov 24 '22 18:11

Mark S