Basically, the reverse of abs. If I have:
基本上就是abs的反面。如果我有:
if($this.find('.pdxslide-activeSlide').index() < slideNum-1){ slideNum = -slideNum }
console.log(slideNum)
No matter what console ALWAYS returns a positive number. How do I fix this?
无论哪种控制台总是返回一个正数。我怎么修复这个?
If I do:
如果我做的事:
if($this.find('.pdxslide-activeSlide').index() < slideNum-1){
_selector.animate({left:(-slideNum*sizes.images.width)+'px'},750,'InOutPDX')
}
else{
_selector.animate({left:(slideNum*sizes.images.width)+'px'},750,'InOutPDX')
}
it works tho, but it's not "DRY" and just stupid to have entire block of code JUST for for a -
它可以工作,但它不是“干的”,只是愚蠢的有一整块代码只是为了一个-。
9 个解决方案
#1
121
Math.abs(num) => Always positive
-Math.abs(num) => Always negative
You do realize however, that for your code
但是,您确实意识到,这是为了您的代码
if($this.find('.pdxslide-activeSlide').index() < slideNum-1){ slideNum = -slideNum }
console.log(slideNum)
If the index found is 3 and slideNum is 3,
then 3 < 3-1 => false
so slideNum remains positive??
如果发现的指数是3,而slideNum是3,那么3 < 3-1 => false so slideNum仍然是正数??
It looks more like a logic error to me.
在我看来,这更像是一个逻辑错误。
#2
62
The reverse of abs is Math.abs(num) * -1
.
abs的反面是Math.abs(num) * -1。
#3
15
The basic formula to reverse positive to negative or negative to positive:
由正到负或由负到正的基本公式:
i - (i * 2)
#4
5
Are you sure that control is going into the body of the if
? As in does the condition in the if
ever hold true? Because if it doesn't, the body of the if
will never get executed and slideNum
will remain positive. I'm going to hazard a guess that this is probably what you're seeing.
你确定控制会进入if体吗?就像if中的条件是否成立一样?因为如果没有,if的主体将永远不会被执行,而slideNum将保持正值。我猜这可能就是你们看到的。
If I try the following in Firebug, it seems to work:
如果我在Firebug中尝试以下方法,效果似乎不错:
>>> i = 5; console.log(i); i = -i; console.log(i);
5
-5
slideNum *= -1
should also work. As should Math.abs(slideNum) * -1
.
slideNum *= -1也可以。和Math.abs(slideNum) * -1一样。
#5
4
To get a negative version of a number in JavaScript you can always you the ~
bitwise operator.
要在JavaScript中获得一个数字的负版本,您始终可以使用~位运算符。
For example, if you have a = 1000
and you need to convert it to a negative you could do the following:
例如,如果你有a = 1000,你需要把它转换成负数,你可以这样做:
a = ~a + 1;
a = ~a + 1;
which would result in a
being -1000.
结果是-1000。
#6
2
If you don't feel like using Math.Abs * -1 you can you this simple if statement :P
如果你不想用数学。你可以用简单的if语句:P
if (x > 0) {
x = -x;
}
Of course you could make this a function like this
当然你可以把它写成这样的函数。
function makeNegative(number) {
if (number > 0) {
number = -number;
}
}
makeNegative(-3) => -3 makeNegative(5) => -5
makeNegative(-3) => -3 makeNegative(5) => -5
Hope this helps! Math.abs will likely work for you but if it doesn't this little
希望这可以帮助!数学。abs很可能对你有用,但如果不是的话
#7
1
Use 0 - x
用0 - x
x being the number you want to invert
x是要求倒数的数
#8
0
var x = 100;
var negX = ( -x ); // => -100
#9
0
var i = 10;
i = i / -1;
Result: -10
结果:-10
var i = -10;
i = i / -1;
Result: 10
结果:10
If you divide by negative 1, it will always flip your number either way.
如果你除以- 1,它总是会翻转你的数字。
#1
121
Math.abs(num) => Always positive
-Math.abs(num) => Always negative
You do realize however, that for your code
但是,您确实意识到,这是为了您的代码
if($this.find('.pdxslide-activeSlide').index() < slideNum-1){ slideNum = -slideNum }
console.log(slideNum)
If the index found is 3 and slideNum is 3,
then 3 < 3-1 => false
so slideNum remains positive??
如果发现的指数是3,而slideNum是3,那么3 < 3-1 => false so slideNum仍然是正数??
It looks more like a logic error to me.
在我看来,这更像是一个逻辑错误。
#2
62
The reverse of abs is Math.abs(num) * -1
.
abs的反面是Math.abs(num) * -1。
#3
15
The basic formula to reverse positive to negative or negative to positive:
由正到负或由负到正的基本公式:
i - (i * 2)
#4
5
Are you sure that control is going into the body of the if
? As in does the condition in the if
ever hold true? Because if it doesn't, the body of the if
will never get executed and slideNum
will remain positive. I'm going to hazard a guess that this is probably what you're seeing.
你确定控制会进入if体吗?就像if中的条件是否成立一样?因为如果没有,if的主体将永远不会被执行,而slideNum将保持正值。我猜这可能就是你们看到的。
If I try the following in Firebug, it seems to work:
如果我在Firebug中尝试以下方法,效果似乎不错:
>>> i = 5; console.log(i); i = -i; console.log(i);
5
-5
slideNum *= -1
should also work. As should Math.abs(slideNum) * -1
.
slideNum *= -1也可以。和Math.abs(slideNum) * -1一样。
#5
4
To get a negative version of a number in JavaScript you can always you the ~
bitwise operator.
要在JavaScript中获得一个数字的负版本,您始终可以使用~位运算符。
For example, if you have a = 1000
and you need to convert it to a negative you could do the following:
例如,如果你有a = 1000,你需要把它转换成负数,你可以这样做:
a = ~a + 1;
a = ~a + 1;
which would result in a
being -1000.
结果是-1000。
#6
2
If you don't feel like using Math.Abs * -1 you can you this simple if statement :P
如果你不想用数学。你可以用简单的if语句:P
if (x > 0) {
x = -x;
}
Of course you could make this a function like this
当然你可以把它写成这样的函数。
function makeNegative(number) {
if (number > 0) {
number = -number;
}
}
makeNegative(-3) => -3 makeNegative(5) => -5
makeNegative(-3) => -3 makeNegative(5) => -5
Hope this helps! Math.abs will likely work for you but if it doesn't this little
希望这可以帮助!数学。abs很可能对你有用,但如果不是的话
#7
1
Use 0 - x
用0 - x
x being the number you want to invert
x是要求倒数的数
#8
0
var x = 100;
var negX = ( -x ); // => -100
#9
0
var i = 10;
i = i / -1;
Result: -10
结果:-10
var i = -10;
i = i / -1;
Result: 10
结果:10
If you divide by negative 1, it will always flip your number either way.
如果你除以- 1,它总是会翻转你的数字。