views:

209

answers:

2

Hello I have problem to put together animations of two separate objects to second one start when first one ends. I tried to use callback but it seems that i make some syntax misteakes which crash jQuery or cause some unexpected behaviour. It seems that i'm stuck so I'd like to ask You for best way to put these animations together to act the way I want.

in mouseenter 1st .pp grows, 2nd .tt fade in

in mouseleave 1st .tt fade out, 2nd .pp shrink

It's alsp relevant that animations doesn't pile up, i mean here animation called by one event doesnt wait until other animation in progress will end. In generall exactly what is below but yo be animated one after another, not simultanously.

$('.pp').bind({
    mouseenter: function() 
    {
        $(this).animate({ 
            width: $(this).children(".tt").outerWidth(),
            height: $(this).children(".tt").outerHeight()
          },{duration:1000,queue:false} );

        $(this).children(".tt").animate({ 
            opacity: 1.0
          }, {duration:1000,queue:false});  

    },
    mouseleave: function() 
    {
        $(this).children(".tt").animate({ 
            opacity: 0
          }, {duration:1000,queue:false});  
        $(this).animate({ 
            width: 17,
            height: 17
          }, {duration:1000,queue:false});  
    }
});
+2  A: 

You need to add a completion callback, like this:

var me = $(this);
me.animate({ 
        width: me.children(".tt").outerWidth(),
        height: me.children(".tt").outerHeight()
    }, { 
        duration: 1000, 
        queue: false 
        complete: function() {
            me.children(".tt").animate(
                { opacity: 1.0 },
                { duration: 1000, queue: false }
            ); 
        }
    });

See the documentation.

SLaks
you cannot use an object for duration and a callback at the same time..
Gaby
@Gaby: Fixed; thanks.
SLaks
@SLaks :) You could also chain the second animation and set the queue to true ..
Gaby
@Gaby: The second animation is animating a different element.
SLaks
@SLaks, you are right of'course... my oversight..
Gaby
@SLaks: Now there is thing i mentioned, i got few of .pp elements if i move mouse over few of them all of them starts to grow instead of shrinking when mouse leave their area
MoreThanChaos
@SLaks: om yhe end it seems that lack of queuing gives better effect
MoreThanChaos
A: 

I think you simply call .animate multiple times like so:

$(this).animate({ 
        width: $(this).children(".tt").outerWidth(),
        height: $(this).children(".tt").outerHeight()
        },{duration:1000,queue:false} ).children(".tt")
        .animate({ 
        opacity: 1.0
        }, {duration:1000,queue:false}); 

Give it a try and see if it works.

George Edison
As I understand, he wants the second animation to start after the first one finishes.
SLaks
Oh, okay. Then the answer above will work for him.
George Edison
This code works exactly as mine, resize and fading is simultanously
MoreThanChaos