我有一個語法問題,因爲我想做一件很簡單的事情。使用.css
將負值應用於變量。應用負值與變量使用.css與jquery
這裏喲有代碼:
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);
我想忘記figureImageMarginLeft
和figureImageMarginTop
。那麼,它有可能是這樣的嗎?
figureImage.css('margin-left', -figureImageMarginLeft);
你如何正確地寫呢?
你不需要做'(0-(figureImageWidth/2 ))',''-figureImageWidth/2'很好。 –
如果你不知道'figureImageMarginLeft'是** - ve **還是** + ve **,那麼你可以這樣做:'-Math.abs(figureImageMarginLeft)'總是得到負數。 –
@AshishKumar這工作完美,但我想知道未來如何將這個負值應用於.css字段,如果我需要它。 –