Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increasing css values with jquery

Tags:

jquery

css

How can I increase css values with jquery?

I want to increase values such as top and left, this is what i tried but does not work:

var left = 5;
$(.object).css("left" + 5);

the thing is that i need the value to be an integer rather than a string like "5px". I need 5 so that i can change it with numerical expressions.

like image 484
jcrowley Avatar asked Jul 29 '26 17:07

jcrowley


1 Answers

From .css() documentation:

As of jQuery 1.6, .css() accepts relative values similar to .animate(). Relative values are a string starting with += or -= to increment or decrement the current value. For example, if an element's padding-left was 10px, .css( "padding-left", "+=15" ) would result in a total padding-left of 25px.

So, you can increment left with your js variable with:

var left = 5;
$(object).css('left', '+=' + left);
like image 200
freejosh Avatar answered Aug 01 '26 06:08

freejosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!