Using fadein and append

JavascriptJqueryFade

Javascript Problem Overview


I am loading JSON data to my page and using appendTo() but I am trying to fade in my results, any ideas?

$("#posts").fadeIn();
$(content).appendTo("#posts");

I saw that there is a difference between append and appendTo, on the documents.

I tried this as well:

$("#posts").append(content).fadeIn();

I got it, the above did the trick!

But I get "undefined" as one of my JSON values.

Javascript Solutions


Solution 1 - Javascript

If you hide the content before you append it and chain the fadeIn method to that, you should get the effect that you're looking for.

// Create the DOM elements
$(content)
// Sets the style of the elements to "display:none"
    .hide()
// Appends the hidden elements to the "posts" element
    .appendTo('#posts')
// Fades the new content into view
    .fadeIn();

Solution 2 - Javascript

I don't know if I fully understand the issue you're having, but something like this should work:

HTML:

<div id="posts">
  <span id="post1">Something here</span>
</div>

Javascript:

var counter=0;

$.get("http://www.something/dir",
    function(data){
        $('#posts').append('<span style="display:none" id="post' + counter + ">" + data + "</span>" ) ;
        $('#post' + counter).fadeIn();
        counter += 1;
    });

Basically you're wrapping each piece of the content (each "post") in a span, setting that span's display to none so it doesn't show up, and then fading it in.

Solution 3 - Javascript

This should solve your problem I think.

$('#content').prepend('<p>Hello!</p>');
$('#content').children(':first').fadeOut().fadeIn();

If you are doing append instead then you have to use the :last selector instead.

Solution 4 - Javascript

You have to be aware that the code doesn't execute linearly. The animated stuff can't be expected to halt code execution to do the animation and then return.


commmand();
animation();
command();  <-- you'll find this runs before the animation finishes

This is because the animation uses set timeout and other similar magic to do its job and settimeout is non-blocking.

This is why we have callback methods on animations to run when the animation is done ( to avoid changing something which doesn't exist yet )


command();
animation( ... function(){
command();
});

Solution 5 - Javascript

$(output_string.html).fadeIn().appendTo("#list");

Solution 6 - Javascript

assuming you have the following in the css defined:

.new {display:none} 

and the javascript should be :

$('#content').append('<p class='new'>Hello!</p>');
$('#content').children('.new').fadeIn();
$('#content').children.removeClass('new');
$('#content').children('.new').hide();

Solution 7 - Javascript

First is convert received data to jQuery Object. Second, hide it immediately. Third, append it to a target node. And, after this all, we can clearly use necessary animation, just like fadeIn :)

jNode = $("<div>first</div><div>second</div>");
jNode.hide();
$('#content').append(jNode);
jNode.fadeIn();

Solution 8 - Javascript

im have a exprensive,for this:

$("dt").append(tvlst.ddhtml);
$("dd:last").fadeIn(700);

Solution 9 - Javascript

I tried what you said did the trick but is not working. it worked with the following code

$("div").append("content-to-add").hide().fadeIn();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser39980View Question on Stackoverflow
Solution 1 - JavascriptKevin GorskiView Answer on Stackoverflow
Solution 2 - JavascriptParandView Answer on Stackoverflow
Solution 3 - JavascriptFirasView Answer on Stackoverflow
Solution 4 - JavascriptKent FredricView Answer on Stackoverflow
Solution 5 - JavascriptBall_csView Answer on Stackoverflow
Solution 6 - JavascriptMichael MartinView Answer on Stackoverflow
Solution 7 - JavascriptSamView Answer on Stackoverflow
Solution 8 - JavascriptslboatView Answer on Stackoverflow
Solution 9 - JavascriptEmanuel SilvaView Answer on Stackoverflow