Doubt about the property "Animation-name"

Asked

Viewed 98 times

2

I am studying all properties of CSS3 and came to me a doubt about the property Animation-name.

The value that is given to this property is either customizable or is a language default value?

Doubt came after reading a code that had the following content:

div {
    width: 100px;
    height: 100px;
    background: red;
    position: relative;
    -webkit-animation-name: mymove;  /* Chrome, Safari, Opera */
    -webkit-animation-duration: 5s;  /* Chrome, Safari, Opera */
    animation-name: mymove;
    animation-duration: 5s;
}

/* Chrome, Safari, Opera */
@-webkit-keyframes mymove {
    from {left: 0px;}
    to {left: 200px;}
}

@keyframes mymove {
    from {left: 0px;}
    to {left: 200px;}
}

In which it made me think that the value mymove is a user defined value and not a language predefined.

1 answer

2


Yes, this is a custom name that should reference the name given to the directive @keyframe. According to W3 specification:

The estate Animation-name sets a list of animations that apply. Each name is used to select the keyframe in the rule that provides property values for the animation. If the name does not match any keyframe in the rule, there are no properties to be animated and the animation will not be executed. Also, if the animation name is None, then there will be no animation. This can be used to replace the cascading animations. If multiple animations try to modify the same property, then the animation closest to the end of the name list wins.

Possible values for this property are:

none - None keyframe is specified, so there is no animation. Any other specified animation property will have no effect.

<custom-ident> - The animation will use the keyframes specified by property (comma-separated), if any. If such keyframes do not exist, there is no animation.

See the following example: Three keyframes are specified - slide-right, slide-up, slide-down. However two of them change the same property (margin-top). If the order of the keyframes declaration slide-up and slide-down is changed in property animation-name, is worth the one declared last.

div {
  animation-name: slide-right, slide-up, slide-down;
  animation-duration: 2s;
}

@keyframes slide-down {
  from {
    margin-top: 0px;
  }
  50% {
    margin-top: 110px;
    opacity: 1;
  }
  50% {
    opacity: 0.9;
  }
  to {
    margin-top: 200px;
  }
}

@keyframes slide-right {
  from {
    margin-left: 0px;
  }
  50% {
    margin-left: 110px;
    opacity: 1;
  }
  50% {
    opacity: 0.9;
  }
  to {
    margin-left: 200px;
  }
}

@keyframes slide-up {
  from {
    margin-top: 200px;
  }
  50% {
    margin-top: 110px;
    opacity: 1;
  }
  50% {
    opacity: 0.9;
  }
  to {
    margin-top: 0px;
  }
}
<div style="height: 100px; width: 100px; background-color: #ff0000;"></div>

Read the full specification on: http://dev.w3.org/csswg/css-animations/#Animation-name

  • Thank you Marcusvinicius, another question about an excerpt from your text: If multiple animations try to modify the same property, then the animation closest to the end of the name list wins. So this means that an element can only receive one type of animation, and the previous ones would be discarded? That is, the pattern is only one animation per element?

  • No, you can combine the animations, but if several of them modify for example margin-left then there is conflict. In that case, the last specified that works.

  • Let me get this straight. So if I have three slide1 keyframes, slide2 keyframes, slide3 keyframes it will perform only the keyframe slide3?

  • Take a look at the answer, I made an example there. Change the order of the slide-down and slide-up keyframes and you will see that only one of the two works because they change the same property (margin-top). The slide-right always works, regardless of the order, as it has no property conflict with the other two.

  • Got it. In case it’s working just the slide-right and the slide-up right? slide-right the keyframe would not descend, but would only go to the right or vice versa.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.