最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JqueryJavascript : Negative of a Variable - Stack Overflow

programmeradmin9浏览0评论

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.

Thanks again for any direction you can provide.

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.

Thanks again for any direction you can provide.

Share Improve this question asked Feb 11, 2011 at 16:30 RrryyyaaannnRrryyyaaannn 2,5975 gold badges19 silver badges15 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 9

There should be no problems if you put parenthesis around the value you want to negate:

if (imageMargin == -(numberOfFrames*frameWidth-400) )
   ...

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));

How about subtracting it from zero?

if (imageMargin == (0-(numberOfFrames*frameWidth-400)) )
发布评论

评论列表(0)

  1. 暂无评论