The animation-play-state property

The animation-play-state property allows you to set the animation state: it is playing or paused.

Syntax

cssselector {
  animation-play-state: paused | running;
}

Values

Value Description
paused An animation is paused.
running An animation is playing.

Default value: running.

Example

In this example, the animation will start when the mouse hovers over the element and stop when the mouse is removed from the element:

html<div id="elem"></div>
css@keyframes move {
  from {
    width: 200px;
  }
  to {
    width: 400px;
  }
}

#elem {
  animation-play-state: paused;
  animation-duration: 1s;
  animation-name: move;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  border: 1px solid black;
  height: 50px;
}
#elem:hover {
  animation-play-state: running;
}

Результат:

Example . Hovering over another element

And now the animation will be launched by hovering the mouse over another element, and will stop when the mouse is removed from it:

html<div id="target"></div>
<div id="elem"></div>
css@keyframes move {
  from {
    margin-left: 0;
  }
  to {
    margin-left: 400px;
  }
}

#target {
  border: 1px solid red;
  margin-bottom: 10px;
  width: 50px;
  height: 50px;
}

#elem {
  animation-play-state: paused;
  animation-duration: 3s;
  animation-name: move;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  border: 1px solid black;
  width: 50px;
  height: 50px;
}

#target:hover + #elem {
  animation-play-state: running;
  border: 1px solid green;
}

Результат:

See also