What's the syntax for using += with a variable when using the .animate function in jQuery
I'm trying to animate the right margin of an element using += to conti开发者_运维知识库nually 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.
$('.wide').animate({"right" : '+=' + variable + 'px'}, scrollSpeed, scrollEase);
Just concatenate a string.
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.
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);
精彩评论