views:

331

answers:

2

Sorry if this is an easy one, I'm new to jQuery. I am using the Flip! plugin to rotate a div in place. I would also like to move the div while it is rotating. Currently I am doing this:

        $("#flipDiv").flip({
            direction:'lr',
            content:newContent
        });
        $("#flipDiv").animate({
            'marginLeft' : "+=150px"
        });

It does flip the div, but the animation does not run until the flipping is done, and even then it goes all at once. Since I am not very familiar with javascript, I do not know if this is a limitation of the language, or of this particular plugin, or if I am just doing it wrong. Does anyone know of a better way something like this can be done?

+1  A: 
$("#flipDiv").flip({
    direction:'lr',
    content:newContent,
    onAnimation: function(){
         $(this).animate({
               left: '+=150px'
         });
    }
})

You can also use onBefore instead of onAnimation if you want the move to begin at the same time as the flip instead of half way through.

Mark
Funny, that doesn't seem to move it at all. if I change $(this) to $("#flipDiv") I have the same problem I started with. According to the plugin documentation the onAnimation function is called synchronously when the animation is half done. I would like them to both happen at the same time.
CaptnCraig
My problem is that it still does not appear to move at all until after the rotation is done, no matter when I call animate()
CaptnCraig
Have you tried using onBefore instead of onAnimation?
Mark
yeah, it flips it, and then it instantly jumps right at the end.
CaptnCraig
Have you tried adding a speed to .animate? .animate({ left: '+=150px'}, 'medium');Also if you use left: '150px' it should animate the position of the element to 150px rather than move it at 150px increments.
Mark
No matter how I tell it to animate, the problem is the same. It appears that the flip call is not letting animate() run until it is all done.
CaptnCraig
Try the above solution queue: false
Mark
+2  A: 

I'm assuming that flip must just be using animate under the covers, which by default allows you to queue up multiple animations which then play out in order. To avoid this you can tell an given animation to not queue itself and execute immediately.

So try this:

$("#flipDiv").flip({
 direction:'lr',
 content:newContent
});
$("#flipDiv").animate({
 'marginLeft' : "+=150px"
}, {queue: false});

For more information have a look at the API documentation on animate, as well as queue and dequeue.


Edit: Ok, looking at the source of the flip plugin I now believe the problem is that when you run flip it actually hides the element (#flipDiv in your case), creates a placeholder clone that it does its flip animation on & then re-shows the original div in its final state. This means that even if you make the animation happen at the same time, it will be animating the hidden div, so it will suddenly reappear either half way through the animation or after it has finished.

You could try instead to animate the clone, which you can access from the onBefore callback like so:

$("#flipDiv").flip({
  direction:'lr',
  content:newContent,
  onBefore: function(clone) {
    clone.animate({ 'marginLeft' : "+=150px" }, {queue: false});
  })
});

Of course that won't actually move the original div, so when the flip finishes your div will still be in the old location, so you'll need to shift that too. If you expect the animate to take the same (or less) time as the flip then you could just set the original's marginLeft directly, otherwise you could animate it too so it stays roughly in sync.

Alconja
That is probably the case. This should work.
Mark
Hmmm. That does sound like the problem, but adding the queue bit does not change the behavior at all. Is it possible the plugin is somehow suspending the animation in progress?
CaptnCraig
Just curious but what browser are you testing in?
Mark
I'm using Firefox 3.5.7
CaptnCraig
Same behavior in Chrome
CaptnCraig
If I use this code to move any other div, it will move at the same time as the flip animation. If I try to move the same div, it doesn't move till after.
CaptnCraig
@CaptnCraig: See my new edit for another idea...
Alconja
Awesome. Thanks for figuring that out. I just animate #flipDiv with the exact same call that moves clone and it all works out well. Thank you!
CaptnCraig