Repeat animation after javascript made css

I am in the process of creating a roulette game, but I have a problem that my "animation" (or what you call it) can play only once. I don't have that much experience with javascript and I don't know how to solve my problem.

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

function onClick() {
  this.removeAttribute('style');
  var 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;
}
.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>
Run codeHide result

+5
source share
1 answer

You must change the degrees on each 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>
Run codeHide result

Or set a timeout to make sure the attribute is deleted before it is added:

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>
Run codeHide result
+3
source

Source: https://habr.com/ru/post/1670456/


All Articles