Skip to content Skip to sidebar Skip to footer

JQuery Stop Animation Method

I have this spinning div and I want it to stop when I click on a button by using the .stop() method, but for some reason it doesn't work. I hope you can shed some light on this iss

Solution 1:

It's a CSS animation, not a jQuery animation so you just need to set the css animation property to "none":

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

Solution 2:

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>

Solution 3:

Your animation is being initialised in the CSS. Therefore you must stop it by CSS. $(".player-disc").css("animation-play-state", "paused");


Solution 4:

Try this code...

 $("button").click(function(){
$('.player-disc').css('animation', 'none');
 });

Fiddle


Post a Comment for "JQuery Stop Animation Method"