If I have
<div id="curve" style="position:relative; height:100px; width:100px; />
How would I make it move on a curve? I've googled and everything but can't seem to find another example that would call two functions at once. This is the kind of code I would like, but doesn't work:
$('#curve').click(function () {
$(this).animate(
{
top: 400,
left = $(this).left() + $(this).left()*$(this).left()
},
'slow',
function() { $(this).animate( { left: 600 }, 'fast' ); }
);
});
Even if that's not correct code, I believe animate only takes "destinations" for something to go to, so a dynamic destination wouldn't work I think. What am I looking for to make this work?
EDIT:: I'll definitely pick up that plugin, but I'm also wonder why this bit of code doesn't work as I'd expect it to.
Here's another attempt using a for loop and the delay method
$('#curve').click(function () {
for (var i=0; i<400; i++ )
{
$(this).delay(1000);
$(this).css( { top: i, left: i*1.5 } );
}
});
Except it just instantly goes to that position, no delay or anything. so if it was starting at [0,0], as soon as I click it it teleports to [400,600]. Why doesn't the delay work?
There's a tiny script, just for animation which isn't in straight lines, called pathAnimator
It's very very small and super efficient. and you don't even need jQuery ;)
I think that this time, you have to recalculate animated curve part by part in js and then do it by moving by little parts (= you probably could find plugin OR you'll have to do all the math by yourself)
Edit 2: Previously added link was moved=> http://foxparker.wordpress.com/2009/09/22/bezier-curves-and-arcs-in-jquery/. Thanks, Zach.
Edit 1: this intrigued me, so I did little google research - just as I thought: plugin ready for use here: http://foxparker.wordpress.com/2009/09/22/bezier-curves-and-arcs-in-jquery/
Here's a simple little library I wrote that allows arbitrary cubic Bézier curves for an animation path, and even calculates the rotation angle for you. (The library isn't polished or documented yet, but it shows how easy it is to stand on the shoulders of SVG's DOM even when you have no SVG elements in your page.)
http://phrogz.net/SVG/animation_on_a_curve.html
You can either edit the code and watch the curve/animation change, or edit the curve and see the code update.
In case my site is down, here's the relevant code for posterity:
The jQuery.path plugin is what you want:
Example: animate along an arc
Example: animate along a sine wave
Are you using jQuery 1.4?
You'll require the easing plugin for this to work: http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js
E.g. http://jsbin.com/ofiye3/2