[英]How to shorten multiple if else with increasing counters
I have been finding how to shorten multiple if else with counters but couldn't find anything that is related to my situation. 我一直在寻找如何缩短计数器的倍数,但是找不到与我的情况有关的任何东西。
Currently I have too many if statements for each increasing counter. 目前,每个递增计数器的if语句过多。
Is there a way to achieve that? 有办法实现吗?
Below is my code : 下面是我的代码:
$(".gift").each(function(){
var i = 1;
$(this).on("click", function(){
if(i===1){
TweenMax.fromTo(this, 1, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x"})
}
if(i===2){
TweenMax.fromTo(this, 0.9, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x"})
}
if(i===3){
TweenMax.fromTo(this, 2/i, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x"})
}
if(i===4){
TweenMax.fromTo(this, 2/i, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x"})
}
if(i===5){
TweenMax.fromTo(this, 2/i, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x"})
}
if(i===6){
TweenMax.fromTo(this, 2/i, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x"})
}
if(i===7){
TweenMax.fromTo(this, 2/i, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x"})
}
if(i===8){
TweenMax.fromTo(this, 2/i, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x"})
}
if(i===9){
TweenMax.fromTo(this, 2/i, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x"})
}
if(i===10){
$this = $(this);
TweenMax.fromTo(this, 2/i, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x", onComplete:function(){
$this.css("top", "500px");
$(".gift").each(function(){
$(this).off("click");
});
}})
}
i++;
console.log(i);
});
});
Thanks in advance guys! 在此先感谢大家!
Another solution to "complex" if else statements as suggested in the comments is to use a switch statement: 注释中建议的其他“ ifelse”语句解决方案是使用switch语句:
$(".gift").each(function(){
var i = 1;
$(this).on("click", function(){
switch(i) {
case 1:
TweenMax.fromTo(this, 1, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x"})
break;
case 2:
TweenMax.fromTo(this, 0.9, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x"})
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
TweenMax.fromTo(this, 2/i, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x"})
break;
case 10:
$this = $(this);
TweenMax.fromTo(this, 2/i, {x:-1}, {x:1, ease:RoughEase.ease.config({strength:8, points:5, template:Linear.easeNone, randomize:false}) , clearProps:"x", onComplete:function(){
$this.css("top", "500px");
$(".gift").each(function(){
$(this).off("click");
});
}})
break;
}
i++;
console.log(i);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.