JavaScript中的正数到负数? [英] Positive Number to Negative Number in JavaScript?

查看:94
本文介绍了JavaScript中的正数到负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,abs的反向。如果我有:

Basically, the reverse of abs. If I have:

if($this.find('.pdxslide-activeSlide').index() < slideNum-1){ slideNum = -slideNum }
console.log(slideNum)

无论什么控制台ALWAYS返回正数。我该如何解决这个问题?

No matter what console ALWAYS returns a positive number. How do I fix this?

如果我这样做:

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 -

推荐答案

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)

如果找到的索引是3且slideNum是3,那么
然后3< 3-1 => false

所以slideNum 保持正面 ??

If the index found is 3 and slideNum is 3,
then 3 < 3-1 => false
so slideNum remains positive??

对我来说这看起来更像是一个逻辑错误。

It looks more like a logic error to me.

这篇关于JavaScript中的正数到负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆