[英]if statement doesn't work for counter variable
I want my script to execute a method, when the scrollCount variable reaches a value > 2. 我希望脚本在scrollCount变量的值达到> 2时执行一个方法。
Code: 码:
$( document ).ready(function() {
var scrolly = 0;
var scrollCount = 0;
console.log(scrollCount);
$('#wizard-next').click(function(e) {
e.preventDefault();
scrolly -= 300;
$( "#wizard-step-1" ).animate({ marginTop: (scrolly+'px') }, 1200);
scrollCount++;
console.log(scrollCount);
});
$('#wizard-prev').click(function(e) {
e.preventDefault();
scrolly += 300;
$( "#wizard-step-1" ).animate({ marginTop: (scrolly+'px') }, 1200);
scrollCount--;
console.log(scrollCount);
});
if(scrollCount > 2) {
console.log('Add method here!');
}
});
The if statement doesn't work. if语句不起作用。 How can I fix that?
我该如何解决?
if(scrollCount > 2) {
console.log('Add method here!');
}
As I'm sure you've noticed from the comments (@Jaromanda), the if statement is only done once. 我确信您已经从注释(@Jaromanda)中注意到了,if语句仅执行一次。 Which of course at that moment is false, since scollCount will be 0.
由于scollCount将为0,因此那一刻当然是假的。
If you want it to be called everytime a click occurs then suround it by a function. 如果您希望每次单击都会调用它,则可以使用函数对其进行包围。
var scrollCountCheck = function(){
if(scrollCount > 2) {
console.log('Add method here!');
}
}
And call this method in each of your click functions. 并在每个点击函数中调用此方法。 right after or before scrollCount++ or scrollCount-- (depending on when you actually intended on calling it)
在scrollCount ++或scrollCount--之前或之后(取决于您实际打算何时调用它)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.