使用jquery动画来控制大奖html旋转

时间:2018-01-19 10:31:06

标签: javascript jquery html css jquery-animate

我使用下面的代码来匹配用户旋转。这是有效但我的问题是当winner匹配一个数字时,旋转旋转仍然在特定的圆圈中结束,然后再为获胜的圆圈添加样式。

我的问题,无论如何,我可以让旋转旋转速度减慢并落在winner上,所以它不必跳到停止后获胜的数字吗?我希望它仍然旋转直到它落在数字上。

$(function() {

  var current = 1;
  var num = 0;
  var max = $('.jackpot').length + 1;
  var rotate = 5;
  var speed = 80;

  function change() {
    var winner = Math.floor((Math.random() * 12) + 1);
    $('.jack-' + current).toggleClass("active-spin spin-default");
    current++;
    if (current == max) {
      current = 1;
      num++;
    }
    if (num == 3) {
      speed = 160;
    }

    if (num != rotate) {
      setTimeout(function() {
        change()
      }, speed);
    } else {
      $('.jackpot').removeClass("active-spin").addClass("spin-default");
      $('.jack-' + winner).addClass("iswin");
      current = winner;
      num = 0;
    }
  }

  $('.play').click(function(event) {
    $('.jackpot').removeClass("iswin").addClass('spin-default');
    setTimeout(function() {
      change()
    }, speed);
  });
});
.jackpot {
  display: inline-block;
  padding: 10px;
  margin: 10px;
  border-radius: 50%;
  border: 1px solid transparent;
  transition: 0.5s;
}

.spin-default {
  border: 1px solid orange;
  background-color: #eee;
}

.jackpot.active-spin {
  border: 1px solid blue;
  background-color: red;
}

.jackpot.iswin {
  border: 1px solid yellow;
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <table>
    <tr>
      <td>
        <div class="jackpot spin-default jack-1">JK</div>
      </td>
      <td>
        <div class="jackpot spin-default jack-2">TR</div>
      </td>
      <td>
        <div class="jackpot spin-default jack-3">JK</div>
      </td>
      <td>
        <div class="jackpot spin-default jack-4">IC</div>
      </td>
      <td>
        <div class="jackpot spin-default jack-5">JK</div>
      </td>
    </tr>
    <tr>
      <td>
        <div class="jackpot spin-default jack-12">TR</div>
      </td>
      <td></td>
      <td></td>
      <td></td>
      <td>
        <div class="jackpot spin-default jack-6">JK</div>
      </td>
    </tr>
    <tr>
      <td>
        <div class="jackpot spin-default jack-11">TR</div>
      </td>
      <td>
        <div class="jackpot spin-default jack-10">JK</div>
      </td>
      <td>
        <div class="jackpot spin-default jack-9">IC</div>
      </td>
      <td>
        <div class="jackpot spin-default jack-8">JK</div>
      </td>
      <td>
        <div class="jackpot spin-default jack-7">IC</div>
      </td>
    </tr>
  </table>

</div>
<button class="play">PLay</button>

1 个答案:

答案 0 :(得分:0)

尝试这一个:) lastRound()是您想要的功能。 findWinner()是您的代码,只是程序的另一部分。

$(function() {

  var current = 1;
  var num = 0;
  var max = $('.jackpot').length + 1;
  var rotate = 5;
  var speed = 80;
  var winner = 0;

  function change() {
    winner = Math.floor((Math.random() * 12) + 1);
    $('.jack-' + current).toggleClass("active-spin spin-default");
    current++;
    if (current == max) {
      current = 1;
      num++;
    }
    if (num == 3) {
      speed = 160;
    }

    if (num != rotate) {
      setTimeout(function() {
        change()
      }, speed);
    } else {
      current = 1;
     lastRound();
    }
  }

  function lastRound(){

     setTimeout(function() {
       $('.jack-' + current).toggleClass("active-spin spin-default");
       if (current == winner){
         findWinner();
       }else{
         current++;
         lastRound();
       }
      }, speed + current * 20);
    }


     function findWinner(){
     $('.jackpot').removeClass("active-spin").addClass("spin-default");
      $('.jack-' + winner).addClass("iswin");
      current = winner;
      num = 0;
  }

  $('.play').click(function(event) {
    $('.jackpot').removeClass("iswin").addClass('spin-default');
    setTimeout(function() {
      change()
    }, speed);
  });
});