0

I have a problem with an animation in jQuery using ajax.

On the click of an button (actually an tag), I call a ajax method, and have the following written inside the success parameter:

success: function(msg) {
  $('.ContentsMainRight').children().fadeOut(500, function() {
    $('.ContentsMainRight').html(msg.d);
    $('.ContentsMainRight').children().fadeIn(1000);
  });
},

This have the following result.

The contents of a div fade out over 500ms as it's supposed to. Then the html contents of the div are swapped, but then the last part did not work as I hoped. The html returned by the ajax method include some text inside a

tag, and a image inside a tag. The result is that the text is automatically displayed instantly with no fadein, but the img that is put fades in over 1 second. Why is the text and image treated differently?

-Daemon

2 Answers 2

2

Most likely, .children() references elements, not text nodes. This means the styles that are applied to them won't persist when the text changes, meaning the text will not have display:none or visibility:hidden (whichever is applied).

Why don't you just fade out the .ContentsMainRight div?

success: function(msg) {
  $('.ContentsMainRight').fadeOut(500, function() {
    $('.ContentsMainRight').html(msg.d);
    $('.ContentsMainRight').fadeIn(1000);
  });
},
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Andy. It seems that the problem is as you say. Even if I remove the fadeIn call completly, the text i input still gets visible. The reason I don't fade out the entire div, is that it would mean that the background image for the div also get's faded out, and that is not what I want. Would it help if I put a second div inside the div with the background, and then fade that div out, and replace the contents inside that div instead of the actual elements inside the div?
I tried putting a new div inside the ContentsMainRight div, and that worked like a charm. Thanks for your insight on this issue.
1

Try:

success: function(msg) {
  $('.ContentsMainRight').fadeOut(500, function() {
    $('.ContentsMainRight').html(msg.d);
    $('.ContentsMainRight').fadeIn(1000);
  });
},

Basically the problem is that you hide the children and then replace the children with your html() call. The replaced content isn't hidden so it's instantly visible, which is what you're seeing.

Another issue is that if there are multiple children you will be replacing all the children on each child's callback. When you call fadeOut() on multiple elements, it's called separately for each element.

To give you an example of what I mean assume:

<ul>
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

Compare:

$("ul").fadeOut(500, function() {
  alert("replacing");
  $(this).html("<li>four</li><li>five</li><li>six</li>").fadeIn(500);
});

with:

$("ul").children().fadeOut(500, function() {
  alert("replacing"); // this will be called three times
  $(this).html("<li>four</li><li>five</li><li>six</li>").children().fadeIn(500);
});

or to make it even more clear:

$("ul").children().fadeOut(500, function() {
  alert("replacing"); // this will be called three times
  $(this).append("<li>four</li><li>five</li><li>six</li>").children().fadeIn(500);
});

2 Comments

Hi Cletus. I only have one element with the ContentsMainRight class, so the replace of several elements in not an issue, but that the replaced content is visible is the problem. Thanks for letting me know the (obvious) reason.
Your example is very good. I fixed this now putting another div inside my ContentsMainRight div, and then fading that div in and out without the call to children. I'm quite new to jQuery so I appreciate the help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.