Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apply negative value with variable using .css with jquery

I have a syntax issue as I want to do something quite simple. Apply a negative value to a variable using .css.

Here yo have the code:

var figureImage = $('.js-image-centering');
var figureImageHeight = figureImage.height();
var figureImageWidth = figureImage.width();
var figureImageMarginLeft = (0-(figureImageWidth/2));
var figureImageMarginTop = (0-(figureImageHeight/2));

figureImage.css('margin-left', figureImageMarginLeft);
figureImage.css('margin-top', figureImageMarginTop);

I would like to forget about figureImageMarginLeft and figureImageMarginTop. So, its it possible to something like this?

figureImage.css('margin-left', -figureImageMarginLeft);

How do you write it correctly?

like image 558
Daniel Ramirez-Escudero Avatar asked Jun 06 '26 17:06

Daniel Ramirez-Escudero


1 Answers

Yes Absolutely. If you do,

var a = 100;
$(".stuff").css("margin-left",-a)

the element would get the rule: margin-left: -100px

like image 200
Steve Robinson Avatar answered Jun 08 '26 05:06

Steve Robinson



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!