Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat animation after its done javascript css

I am in the progress to make a roulette game, but I got a problem that my "animation" only is able to play once. How can I make the animation repeat every time it is clicked?

const ball = document.getElementsByClassName("ball-container")[0]
var deg = 1080;

function onClick() {
  ball.removeAttribute('style');
  ball.setAttribute('style', `-webkit-transform: rotate(${deg}deg)`);
}
.container {
  position: relative;
  background-color: lightgray;
  height: 300px;
  width: 300px;
}

.ball-container {
  height: 300px;
  width: 300px;
  -webkit-transition: -webkit-transform 4s ease-out;
  border-radius: 50%;
  background-color: green;
}

.ball {
  position: absolute;
  top: 0;
  left: 45%;
  background-color: black;
  border-radius: 50%;
  height: 30px;
  width: 30px;
}
<h1>Press black circle</h1>

<div class="container">
  <div class="board"></div>
  <div class="ball-container">
    <div onclick="onClick()" class="ball"></div>
  </div>
</div>
like image 586
PEPEGA Avatar asked Oct 17 '22 16:10

PEPEGA


1 Answers

You have to change the degrees on every run. Such as...

var img = document.querySelector('#ball');
    ball.addEventListener('click', onClick, false);
    

    var deg = 0;

    function onClick() {
        this.removeAttribute('style');

        deg += 1080;

        var css = '-webkit-transform: rotate(' + deg + 'deg);';

        this.setAttribute(
            'style', css
        );           
        
    }
.roulette{
  padding-top: 5em;
    padding-left: 5em;
    
}
.roulette img{

    position: absolute;    
    height: 50em;
    

}
.wheel{
    
    
}
.ball{
    z-index: 1;
    
}
.border{
    
    z-index: 1;
}
#ball {
    -webkit-transition: -webkit-transform 4s ease-out;
    z-index: 1;
    
        
}
<h1>Press the ball</h1>
  <div class="roulette">
        <img class="border" src="http://www.mediafire.com/convkey/0a20/z78myz6x2nk17n76g.jpg">
        <img id="ball" src="http://www.mediafire.com/convkey/8eec/69mn483g0moovs66g.jpg">
        <img class="wheel" id="spin2" src="http://www.mediafire.com/convkey/937f/f4f65gia76l409u6g.jpg">

    </div>

Or set a Timeout to make sure that the attribute is removed before it is added back:

var img = document.querySelector('#ball');
ball.addEventListener('click', onClick, false);


function onClick() {
  that = this;
  this.removeAttribute('style');

  setTimeout(function() {
    var css = '-webkit-transform: rotate(1080deg);';
    that.setAttribute(
      'style', css
    );
  }, 0);
}
.roulette {
  padding-top: 5em;
  padding-left: 5em;
}

.roulette img {
  position: absolute;
  height: 50em;
}

.wheel {}

.ball {
  z-index: 1;
}

.border {
  z-index: 1;
}

#ball {
  -webkit-transition: -webkit-transform 4s ease-out;
  z-index: 1;
}
<h1>Press the ball</h1>
<div class="roulette">
  <img class="border" src="http://www.mediafire.com/convkey/0a20/z78myz6x2nk17n76g.jpg">
  <img id="ball" src="http://www.mediafire.com/convkey/8eec/69mn483g0moovs66g.jpg">
  <img class="wheel" id="spin2" src="http://www.mediafire.com/convkey/937f/f4f65gia76l409u6g.jpg">

</div>
like image 181
Chris Happy Avatar answered Oct 27 '22 01:10

Chris Happy