Jquery/Javascript:变量的负数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4971532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery/Javascript : Negative of a Variable
提问by Rrryyyaaannn
What's the best way to check for the negative of a variable?
检查变量负值的最佳方法是什么?
Here are my variables:
这是我的变量:
var frameWidth = 400;
var imageWidth = parseInt($('#' + divId).find('#inner-image').css('width'), 10);
var imageMargin = parseInt($('#' + divId).find('#inner-image').css('margin-left'), 10);
var numberOfFrames = imageWidth/frameWidth;
I want to perform a check kind of like this:
我想执行这样的检查:
if (imageMargin == -numberOfFrames*frameWidth-400 )
But I don't know how.
但我不知道怎么做。
In other words, if numberOfFrames*frameWidth-400 equals 800, I need it to return -800.
换句话说,如果 numberOfFrames*frameWidth-400 等于 800,我需要它返回 -800。
Thanks again for any direction you can provide.
再次感谢您提供的任何方向。
回答by sth
There should be no problems if you put parenthesis around the value you want to negate:
如果在要否定的值周围加上括号,应该没有问题:
if (imageMargin == -(numberOfFrames*frameWidth-400) )
...
回答by Matt
If you always want a negative value, and you don't know if it'll be positive or negative:
如果您总是想要一个负值,并且您不知道它是正值还是负值:
function getNegativeOf(val) {
return Math.abs(val) * -1;
};
Then use as:
然后用作:
var guaranteedNegativeImageWidth = getNegativeOf(parseInt($('#' + divId).find('#inner-image').css('width'), 10));
回答by Ben Jakuben
How about subtracting it from zero?
从零减去它怎么样?
if (imageMargin == (0-(numberOfFrames*frameWidth-400)) )

