tags:

views:

22

answers:

3

I'm trying to animate the right margin of an element using += to continually add to the margin each time the function is called. I want the value to be a variable though and I can't figure out the syntax.

Here's what I have:

$('.wide')animate({"right" : '+=320px'}, scrollSpeed, scrollEase);

And that works but here's what I'm trying to do:

$('.wide').animate({"right" : +=variable}, scrollSpeed, scrollEase);

I'm not sure what the correct syntax is though.

Thans for your help.

A: 

You need to just use the + operator.

'+=' + variable should work out, though it may not depending on what variable actually IS .

Make sure it's consistent when fully evaluated with the working literal one.

meder
+1  A: 

$('.wide').animate({"right" : '+=' + variable + 'px'}, scrollSpeed, scrollEase);

Just concatenate a string.

Stefan Kendall
Awesome thanks!
A: 

jQuery interprets arguments like this as a string, so you can just concatenate the string. Also, you can omit the "px" for terseness sake. jQuery will figure that out for you.

$('.wide').animate({'right' : '+=' + variable}, scrollSpeed, scrollEase);
sidewaysmilk