Morta 1 Morta 1 - 3 months ago 14
Javascript Question

jQuery stop animation method

I have this spinning div and I want it to stop when I click on a button by using the

method, but for some reason it doesn't work. I hope you can shed some light on this issue for me.

Here is a snippet which performs the animation and demonstrates that the
Stop animation
button does not stop the animation.



$('button').bind('click', function(){
$('.player-disc').stop(true, false);
});

#player {
position: relative;
margin: 30px auto;
height: 300px;
width: 700px;
background-color: #E25822;
-moz-border-radius: 200px;
-webkit-border-radius: 200px;
border-radius: 200px;
}
#player .player-disc {
-moz-animation: spin 5s infinite linear;
-webkit-animation: spin 5s infinite linear;
animation: spin 5s infinite linear;
height: 250px;
width: 250px;
background-color: black;
background-size: 250px;
position: absolute;
top: 50%;
left: 25px;
transform: translateY(-50%);
}

#player .player-disc span {
position: absolute;
width:30px;
height: 30px;
background-color:white;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
cursor: pointer;
}

@keyframes spin {
0% {
transform: translateY(-50%) rotate(0deg);
}
100% {
transform: translateY(-50%) rotate(360deg);
}
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button">Stop animation</button>
<div id="player">
<div class="player-disc">
<span></span>
</div>
</div>





I've also tried any other combinations of
.stop(false, false)
etc.

a jsFiddle

Answer

As others pointed out, it's CSS animation.

To stop it and reset disc to its original position use (this also resets animation):

$('.player-disc').css('animation', 'none');

To pause it and not reset disc to its original position use:

$('.player-disc').css('animation-play-state', 'paused');

You can then resume animation using:

$('.player-disc').css('animation-play-state', 'running');

$('#btn1').bind('click', function(){
    $('.player-disc').css('animation', 'none');
});
$('#btn2').bind('click', function(){
    $('.player-disc').css('animation-play-state', 'paused');
});
$('#btn3').bind('click', function(){
    $('.player-disc').css('animation-play-state', 'running');
});
#player {
  position: relative;
  margin: 30px auto;
  height: 300px;
  width: 700px;
  background-color: #E25822;
  -moz-border-radius: 200px;
  -webkit-border-radius: 200px;
  border-radius: 200px;
}
#player .player-disc {
  -moz-animation: spin 5s infinite linear;
  -webkit-animation: spin 5s infinite linear;
  animation: spin 5s infinite linear;
  height: 250px;
  width: 250px;
  background-color: black;
  background-size: 250px;
  position: absolute;
  top: 50%;
  left: 25px;
  transform: translateY(-50%);
}

#player .player-disc span {
  position: absolute;
  width:30px;
  height: 30px;
  background-color:white;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  cursor: pointer;
}

@keyframes spin {
  0% {
    transform: translateY(-50%) rotate(0deg);
  }
  100% {
    transform: translateY(-50%) rotate(360deg);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btn1" type="button">Set animation to none</button>
<button id="btn2" type="button">Pause animation</button>
<button id="btn3" type="button">Play animation</button>
<div id="player">
  <div class="player-disc">
  <span></span>
  </div>
</div>