Javascript DOM CSS Style animationPlayState Property

Introduction

Pausing an animation:

document.getElementById("myDIV").style.animationPlayState = "paused";

Click the buttons to Play/Pause the animation:

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {/*from   www .  j  a  v  a 2 s  .  co m*/
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation: mymove 2s infinite;
  animation-play-state: paused;
}

@keyframes mymove {
  from {left: 0px;}
  to {left: 200px;}
}
</style>
</head>
<body>
<button onclick="myPlayFunction()">Play</button>
<button onclick="myPauseFunction()">Pause</button>

<script>
function myPlayFunction() {
  document.getElementById("myDIV").style.animationPlayState = "running";
}

function myPauseFunction() {
  document.getElementById("myDIV").style.animationPlayState = "paused";
}
</script>
<div id="myDIV"></div>

</body>
</html>

The animationPlayState property specifies whether the animation is running or paused.

We can use this property to pause an animation in the middle of a cycle.

Property Values

Value Description
running Default. Specifies that the animation is running
paused Specifies that the animation is paused
initial Sets this property to its default value.
inherit Inherits this property from its parent element.

The animationPlayState property returns a String representing the animation-play-state property of an element.




PreviousNext

Related