Switch CSS3 animations per click

What is the best way to change the direction of CSS3 animations when clicked without javascript?

Recently, I have been studying checkboxes, checkboxes, and trying to find a way to have only one set of key frames instead of two sets for one going forward and one to return. Is it possible? Or is there a way to do this with one set?

For example, I have the following key frames:

@keyframes moveIt {
  0%   { 
    transform: translateX(0);
    width: $size;
  }
  50%  {
    width: $size*1.2;
  }
  100% {
    transform: translateX(50px);
    width: $size;
  }
}
@keyframes moveItBack {
  100%   { 
    transform: translateX(0);
    width: $size;
  }
  50%  {
    width: $size*1.2;
  }
  0% {
    transform: translateX(50px);
    width: $size;
  }
}

Any way to reduce this to just the first set? Then, when does the return start at 100% and return to 0%?

Here is an example of what I'm trying to accomplish.

+4
source share
2 answers

CSS , - ,

- ,

.bubble {
  ...
  animation: moveIt .25s forwards ease-out;
}
input[type=checkbox]:checked ~ .bubble {
  animation: moveIt .25s backwards ease-out;
}

reset , CSS [1]. , . reset javascript setTimeout , , .

, . , translateX ::

.bubble {
  ...
  transition: all .25s ease-out;
}
input[type=checkbox]:checked ~ .bubble {
  transform: translateX(50px);
}

- - . , . , , .

.bubble {
  ...
  transition: all .25s ease-out;
}
.bubble:after, .bubble:before {
  content:''; 
  position:absolute;
  width:100%; height:100%;
  background:inherit;
  border-radius:inherit;
  animation:'';
}
.bubble:before { animation:size .25s ease-out; }
input[type=checkbox]:checked ~ .bubble:before { animation:''; }
input[type=checkbox]:checked ~ .bubble {
  transform: translateX(50px);
}
input[type=checkbox]:checked ~ .bubble:after {
  animation:size .25s ease-out;
}
@keyframes size {
  50% { transform:scale(1.2); }
}

, !

[1]: , , . , - animation:''; animation:moveIt .25s backwards ease-out; reset it

+4

, .

@keyframes moveIt {
  0%   { 
    transform: translateX(0);
    width: $size;
  }
  25% {
    width: $size*1.2;
  }
  50%  {
    transform: translateX(50px);
  }
  75% {
    width: $size*1.2;
  }
  100% {
    transform: translateX(0);
    width: $size;
  }
}
0

Source: https://habr.com/ru/post/1538765/


All Articles