2017-01-10 8 views
0

カウンタを使用して複数の他のショートを短くする方法を見つけましたが、自分の状況に関連するものは何も見つかりませんでした。カウンターを増やす場合に複数を短くする方法

現在、増加するカウンタごとにif文が多すぎます。

これを達成する方法はありますか?事前にみんなで

$(".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); 

     }); 

    }); 

ありがとう:

以下は私のコードです!

+0

うーん...私= 3からI = 9全て同じに、私ができるよう見て、それを一つの条件に入れますか? – sinisake

+0

'i'が' 3'以上のあらゆる状況がまったく同じであるとすれば、どうしてなぜ '' if''/'else if' /' 'else''(https://developer.mozilla.org)を使用していないのですか? /en-US/docs/Web/JavaScript/Reference/Statements/if...else)structure? 'if(i === 1){...} else if(i === 2){...} else if(i> 2){...}'? –

+0

こんにちはsinasake、私は1行で私は= 3~私は= 9のためにそれを行うには? – Dr3am3rz

答えて

0

コメントで示唆されているようにelse文が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); 

     }); 

    }); 
+0

ありがとうアレックス!私もスイッチを使用することを考えていた!しかし、私はまだそれを使用する方法を知らない多くの例を見てください。 = x – Dr3am3rz

関連する問題