Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery append fadeIn

Tags:

jquery

Similar to: Using fadein and append

But the solutions there aren't working for me. I'm trying:

 $('#thumbnails').append('<li><img src="/photos/t/'+data.filename+'"/></li>').hide().fadeIn(2000); 

But then the whole list fades in at once, not as each item is added. It looks like hide() and fadeIn() are being applied to $('#thumbnails') not the <li>. How would I get them to apply to that instead? This doesn't work either:

$('#thumbnails').append('<li stle="display:none"><img src="/photos/t/'+data.filename+'"/></li>').filter(':last').fadeIn(2000); 

Other suggestions?

like image 733
mpen Avatar asked Jun 11 '09 00:06

mpen


People also ask

How to add fadeIn jQuery?

The jQuery fadeIn() method is used to fade in a hidden element. Syntax: $(selector). fadeIn(speed,callback);

What is fadeIn and fadeOut in jQuery?

You can use the jQuery fadeIn() and fadeOut() methods to display or hide the HTML elements by gradually increasing or decreasing their opacity.

What is fadeIn in JS?

The fadeIn() method gradually changes the opacity, for selected elements, from hidden to visible (fading effect). Note: Hidden elements will not be displayed at all (no longer affects the layout of the page).


2 Answers

Your first attempt is very close, but remember that append() is returning #thumbnails, not the item you just added to it. Instead, construct your item first and apply the hide().fadeIn() before adding it:

$('#thumbnails')     .append($('<li><img src="/photos/t/'+data.filename+'"/></li>')         .hide()         .fadeIn(2000)     ); 

This uses the dollar function to construct the <li> ahead of time. You could also write it on two lines, of course, if that makes it clearer:

var item = $('<li><img src="/photos/t/'+data.filename+'"/></li>')     .hide()     .fadeIn(2000); $('#thumbnails').append(item); 

Edit: Your second attempt is also almost there, but you need to use children() instead of filter(). The latter only removes nodes from the current query; your newly-added item isn't in that query, but is a child node instead.

$('#thumbnails')     .append('<li style="display:none"><img src="/photos/t/'+data.filename+'"/></li>')     .children(':last')     .hide()     .fadeIn(2000); 
like image 71
Ben Blank Avatar answered Sep 29 '22 07:09

Ben Blank


$('<li><img src="/photos/t/'+data.filename+'"/></li>')     .appendTo('#thumbnails')     .hide().fadeIn(2000); 
like image 44
Roman Sklyarov Avatar answered Sep 29 '22 08:09

Roman Sklyarov