views:

47

answers:

2

So I have

VARIABLE = Math.floor(5*Math.random());

Then

.animate({left: '+=VARIABLE'}

but it doesn't work.

In my css my div already has a left attribute. If I do left: newx + 'px' it sends it to the random number I generated, meaning it jumps to the top of the page.

+3  A: 

Variables are not interpolated in strings. Try this:

.animate({left: '+='+VARIABLE}
Ivan Nevostruev
Thanks. That did it :D
Michael
A: 

What do you mean by += a variable? You want to increase the current value of the left position? If so you can also fetch the raw number with the .offset() method, increase it, and use it as the new position.

ikanobori
That's a standard way to use [animate](http://api.jquery.com/animate/). - `Animated properties can also be relative. If a value is supplied with a leading += or -=`
Peter Ajtai