Raz Galstyan
Raz Galstyan

Reputation: 327

How to stop css animation without returning to the initial state?

I need to make the play-pause in the animation without js.

Now when I press the pause. Animation goes to the initial state.

How to reset the animation and then restart from the current state?

The solution to javascript is exist. I would like a solution on css.

#play {
  display: none;
}

#play+label[for=play] {
  display: inline-block;
  color: #fff;
  cursor: pointer;
  font-size: 13px;
  z-index: 99;
  padding: 5px;
  background: #37A000;
  margin-bottom: 15px;
}

#play+label[for=play]>span:nth-of-type(2),
#play:checked+label[for=play]>span:nth-of-type(1) {
  display: none;
}

#play:checked+label[for=play] {
  background: tomato;
}

#play:checked+label[for=play]>span:nth-of-type(2) {
  display: block;
}

.progress {
  height: 15px;
  background: #555;
  position: relative;
}

.progress:after {
  width: 0;
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  background: #37A000;
  animation-play-state: paused;
}

#play:checked+label[for=play]~.progress:after {
  animation: animProgress 7s linear infinite;
}

@keyframes animProgress {
  100% {
    width: 100%;
  }
}
<input type="checkbox" name="play" id="play">
<label for="play">
  <span>Play</span>
  <span>Pause</span>
</label>

<div class="progress"></div>

Upvotes: 0

Views: 411

Answers (2)

Facundo Corradini
Facundo Corradini

Reputation: 3913

Set the animation to the .progress::after with initial animation-play-state: paused, then use the :checked for animation-play-state: running

#play {
  display: none;
}

#play+label[for=play] {
  display: inline-block;
  color: #fff;
  cursor: pointer;
  font-size: 13px;
  z-index: 99;
  padding: 5px;
  background: #37A000;
  margin-bottom: 15px;
}

#play+label[for=play]>span:nth-of-type(2),
#play:checked+label[for=play]>span:nth-of-type(1) {
  display: none;
}

#play:checked+label[for=play] {
  background: tomato;
}

#play:checked+label[for=play]>span:nth-of-type(2) {
  display: block;
}

.progress {
  height: 15px;
  background: #555;
  position: relative;
}

.progress:after {
  width: 0;
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  background: #37A000;
  animation: animProgress 7s linear infinite;
  animation-play-state: paused;

}

#play:checked+label[for=play]~.progress:after {
    animation-play-state: running;
}

@keyframes animProgress {
  100% {
    width: 100%;
  }
}
<input type="checkbox" name="play" id="play">
<label for="play">
  <span>Play</span>
  <span>Pause</span>
</label>

<div class="progress"></div>

Upvotes: 0

delinear
delinear

Reputation: 2795

Move your animation styles into the main :after block and then just switch pause/running when the play button is checked/selected, like so:

#play {
  display: none;
}

#play+label[for=play] {
  display: inline-block;
  color: #fff;
  cursor: pointer;
  font-size: 13px;
  z-index: 99;
  padding: 5px;
  background: #37A000;
  margin-bottom: 15px;
}

#play+label[for=play]>span:nth-of-type(2),
#play:checked+label[for=play]>span:nth-of-type(1) {
  display: none;
}

#play:checked+label[for=play] {
  background: tomato;
}

#play:checked+label[for=play]>span:nth-of-type(2) {
  display: block;
}

.progress {
  height: 15px;
  background: #555;
  position: relative;
}

.progress:after {
  width: 0;
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  background: #37A000;
  animation: animProgress 7s linear infinite;
  animation-play-state: paused;  
}

#play:checked+label[for=play]~.progress:after {
  animation-play-state: running;
}

@keyframes animProgress {
  100% {
    width: 100%;
  }
}
<input type="checkbox" name="play" id="play">
<label for="play">
  <span>Play</span>
  <span>Pause</span>
</label>

<div class="progress"></div>

Upvotes: 4

Related Questions