Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add or Subtract a certain number of pixels from existing position using jquery

I am animating a div that is absolutely positioned. The "top" value is deturmined dynamically, so I need to use jquery to animate -20px of whatever the dyanmic "top" value is.

$("#element").animate({"top" : +-20});

Obviously the above is incorrect, but I need to simply subtract 20 from whatever "top" value the #element already has. Can I do this using the above syntax?

For instance, if #element has a top: -300px, I would need the end result to be -320px.

like image 347
JCHASE11 Avatar asked Jan 30 '13 20:01

JCHASE11


1 Answers

You have to pass it a string, prepending -= to your number:

$("#element").animate({ top: "-=20" });
like image 66
Joseph Silber Avatar answered Oct 05 '22 23:10

Joseph Silber