user444787
user444787

Reputation:

jQuery += a variable help

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.

Upvotes: 1

Views: 155

Answers (2)

supakeen
supakeen

Reputation: 2914

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.

Upvotes: 0

Ivan Nevostruev
Ivan Nevostruev

Reputation: 28723

Variables are not interpolated in strings. Try this:

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

Upvotes: 3

Related Questions