[英]JQuery if statement with negative & positive values
I got two variables in a function: $x= -660px and is fixed and $y which is reduced by 20px each time the function is called. 我在函数中得到两个变量:$ x = -660px并且是固定的,$ y每次调用函数时减少20px。 $y = 220px the first time the function is called.
第一次调用函数时$ y = 220px。
In the function I got an if else statement 在函数中我得到了if else语句
if($y > $x){
//do something
}else{
//do nothing
}
What I'm trying to get is for $y to be reduced as long as its bigger than -660px and then stop. 我想要得到的是,只要它大于-660px就会减少$ y然后停止。
it works fine until $y = -20px. 它工作正常,直到$ y = -20px。 ie.
即。 the if statement 'does something' until $y = -20px and then it stops but doesn't execute the 'else' statement.
if语句'做某事'直到$ y = -20px然后它停止但不执行'else'语句。
I can't find out what's wrong with it. 我不知道它有什么问题。 Is this correct or might my problem come from somewhere else?
这是正确的还是我的问题可能来自其他地方?
Shouldn't it be if($y < $x)
since they are negative values? 不应该是
if($y < $x)
因为它们是负值吗?
This works: 这有效:
$(function(){
var x=0,y=-660;
var inter = setInterval(function(){
if(y < x)
subtract();
else
clearInterval(inter);
},200);
function subtract(){
x = x-20;
$('body').append('<br />x='+x+' y='+y);
}
});
Working with px values: 使用px值:
$(function(){
var x="0px",y="-660px";
var inter = setInterval(function(){
if(parseInt(y) < parseInt(x))
subtract();
else
clearInterval(inter);
},200);
function subtract(){
x = (parseInt(x)-20)+"px";
$('body').append('<br />x='+x+' y='+y);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.