views:

3511

answers:

4

With my jquery I'm trying to make the transition from a message to a loading function easy on the eyes by animate the opasity of the message out, inserting the loading.gif and animating the opacity back in. It fails.

$('#powerSearchSubmitButton').click(function(ev) {
    startLoad();
    return false;
});
function startLoad() {
    $('.message').each(function(i) {
        $(this).animate({opacity: 0}, 500, function() {
            $(this).html("<img src=\"/content/pics/loadingBig.gif\" alt=\"loading\" style=\"opacity:0\"/>");
            $(this).animate({opacity: 1},500);
        });
    };
    return true;
};

When I leave out the .html() call, it works fine (except off course the image is not there; So I think that it is beacuse the html isn't inserted with opacity:0; But when I inserted it with style="opacity:0" it cannot fade back in...

What am I doing wrong?

A: 

Apply the animation to a container instead, or use the .css() method to set the opacity (inline styles might interfere with jQuery's ability to animate the corresponding properties).

millenomi
the animation already IS on a container ( div with class "message").
borisCallens
A: 

Don't give the image style="opacity:0;" - it wont be visible while it's parent is hidden, and, as you've found out, it will remain at 0 opacity once the parent has faded back in.

You could also use jQuery's fadeIn and FadeOut functions instead of manually animating.

Simon
thanks, fadeIn and fadeOut are indeed exactly the same. Internally they use the same as I did there.
borisCallens
+2  A: 

this works for me link text

redsquare
Indeed it seems to work..I copy pasted it and it works here too. It is when I replace the image with my image (slightly bigger 66px;66px) I get the choppy fadeIn. When I do the fadeIt a second time it goes smoothly though. Could it be because of the implied resize?
borisCallens
btw, this pastme is handy :) How long does the code stay there?
borisCallens
pastebin.me code stays foreever according to the author. Check out jsbin.com also
redsquare
boris, its prob down to caching the image - you could preload the image to ensure the browser has loaded it, otherwise the image tag has a load method which you could hookup and then run the fadeIn only when the image has loaded.
redsquare
the load hook will be usefull in general. Good idea. But I noticed that whenever the image is loaded, the container "jerks" the height to a fitting height. Any idea how I can prevent this and rather show it slide to fit?
borisCallens
you could try SlideDown/Up maybe
redsquare
A: 

Try this:

$(this).fadeOut(500, function() {
     $(this).html("<img src='content/pics/loadingBig.gif' alt='loading'/>");
    });
$(this).fadeIn(500);
Nathan Long
This is almost exactly the code I would use, except I have had issues where the fadeIn() starts before the inside html has been fully rendered. To solve that I put the fadeIn() *inside* the fadeOut()'s callback function, immediately after the .html() method.
Cirieno
Yes, if you check previous answers you can see that I tried that.It still gives me an IN YOUR FACE load of the loading gif in all browsers but FF
borisCallens