开发者

jquery syntax for making positive variable integer negative

开发者 https://www.devze.com 2023-03-26 09:14 出处:网络
I am new to js and a bit stuck (with something that is probably very ease to fix) I have worked out the height of a div, but I want the variable to be a negative value (so i can centre something hori

I am new to js and a bit stuck (with something that is probably very ease to fix)

I have worked out the height of a div, but I want the variable to be a negative value (so i can centre something horizontally)

Here is the wrong jquery

var modal_height = $('#modal div').outerHeight()/2;
$('#modal div').css('m开发者_开发问答arginTop','-'+modal_height);

Basically as you can see im trying to make the final marginTop value a negative one (i.e. adding a minus sign before the variable)

What is the correct syntax?


You need to use the unary minus operator: -modal_height.
This operator returns a negative number.
(If the original number is already negative, it will return a positive number instead)

0

精彩评论

暂无评论...
验证码 换一张
取 消