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

I animate a div that is absolutely positioned. The "upper" value is dynamically untwisted, so I need to use jquery to animate -20px any value of the "upper" dyanmic value.

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

Obviously this is not true, but I just need to subtract 20 from any "top" value that the #element element already has. Can I do this using the syntax above?

For example, if #element has a vertex: -300px, I need the final result: -320px.

+6
source share
1 answer

You must pass it a string by adding -= to your number:

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

All Articles