Detailed explanation of the usage of the animation attribute of CSS3:
animation is translated into Chinese as "animation". Indeed, the animation attribute is used to define the animation effect of the element. Of course, it is rougher than animations produced using flash or js, but it can meet our basic animation needs, so it is necessary to master this attribute.
1. Basic knowledge:
Before reading the following article, it is recommended to refer to the detailed explanation of @keyframes usage in CSS3 in advance.
Using the transition attribute can achieve animation transition effects, but this attribute has a disadvantage, that is, the transition process from the initial value to the end value is poorly controllable, which means that the animation effect cannot be controlled in more detail, and The animation attribute can be combined with the animation name defined by @keyframes to control the animation transition process in more detail. This attribute is a composite attribute like border, background and other attributes.
For information about the transition attribute, please refer to the detailed explanation of the transition attribute of CSS3.
Syntax structure:
animation:[[ animation-name ] || [ animation-duration ] || [ animation-timing-function ] || [ animation-delay ] || [ animation-iteration-count ] || [ animation-direction ]] [ , [ animation-name ] || [ animation-duration ] || [ animation-timing-function ] || [ animation-delay ] || [ animation-iteration-count ] || [ animation-direction ] || [animation-play-state] || [animation-fill-mode]]*
Parameter analysis:
1.animation-name: This attribute specifies the animation name applied to the element. This name is defined by @keyframes.
2.animation-duration: This attribute is used to specify the duration of animation.
3.animation-timing-function: Used to specify the transition type of animation.
4.animation-delay: used to specify the delay time for the animation to start executing.
5.animation-iteration-count: used to specify the number of repetitions of animation.
6.animation-direction: Used to specify whether reverse movement will occur in the animation loop.
7.animation-play-state: Specifies whether the animation is running or paused.
8.animation-fill-mode: Specifies the state outside the object animation time.
Special instructions:
1. If multiple sets of attribute values are provided, separate them with commas.
2. The corresponding script feature is animation.
Code example:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="//m.sbmmt.com/" /> <title>php中文网</title> <style type="text/css"> div{ width:100px; height:100px; background:red; position:relative; animation:theanimation 5s infinite alternate; -webkit-animation:theanimation 5s infinite alternate ; -moz-animation:theanimation 5s infinite alternate ; -o-animation:theanimation 5s infinite alternate ; -ms-animation:theanimation 5s infinite alternate ; } @keyframes theanimation{ 0% {left:0px;} 100% {left:200px;} } @-webkit-keyframes theanimation{ 0% {left:0px;} 100% {left:200px;} } @-moz-keyframes theanimation{ 0% {left:0px;} 100% {left:200px;} } @-o-keyframes theanimation{ 0% {left:0px;} 100% {left:200px;} } @-ms-keyframes theanimation{ 0% {left:0px;} 100% {left:200px;} } </style> </head> <body> <div></div> </body> </html>
The above code can set the left attribute value of the div element to animate the transition from 0px to 200px, and can repeat the cycle infinitely, and can perform reverse movement.
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="//m.sbmmt.com/" /> <title>php中文网</title> <style type="text/css"> div{ width:100px; height:100px; background:red; position:relative; animation:theanimation 4s infinite alternate; -webkit-animation:theanimation 4s infinite alternate ; -moz-animation:theanimation 4s infinite alternate ; -o-animation:theanimation 4s infinite alternate ; -ms-animation:theanimation 4s infinite alternate ; } @keyframes theanimation{ 0%{top:0px;left:0px;background:red;} 25%{top:0px;left:100px;background:blue;} 50%{top:100px;left:100px;background:yellow;} 75%{top:100px;left:0px;background:green;} 100%{top:0px;left:0px;background:red;} } @-moz-keyframes theanimation{ 0% {top:0px;left:0px;background:red;} 25%{top:0px;left:100px;background:blue;} 50%{top:100px;left:100px;background:yellow;} 75%{top:100px;left:0px;background:green;} 100%{top:0px;left:0px;background:red;} } @-webkit-keyframes theanimation{ 0%{top:0px;left:0px;background:red;} 25%{top:0px;left:100px;background:blue;} 50%{top:100px;left:100px;background:yellow;} 75%{top:100px;left:0px;background:green;} 100%{top:0px;left:0px;background:red;} } @-o-keyframes theanimation{ 0%{top:0px;left:0px;background:red;} 25%{top:0px;left:100px;background:blue;} 50%{top:100px;left:100px;background:yellow;} 75%{top:100px;left:0px;background:green;} 100%{top:0px;left:0px;background:red;} } </style> </head> <body> <div></div> </body> </html>
The above code is more complicated than the first one. Here is an introduction to its running process.
1. The total time of the entire animation is set to 4 seconds.
2.@keyframes defines four stages of animation. Set the left attribute value from 0 to 100px in the 0%-25% time period, and convert the background color from red to blue, 25%-50%, 50%-75 The same is true for % and 75%-100%.
3. And can achieve infinite loop and reverse motion effects.
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="//m.sbmmt.com/" /> <title>php中文网</title> <style type="text/css"> div{ width:100px; height:100px; background:red; position:relative; animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate; -webkit-animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate; -moz-animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate; -o-animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate; -ms-animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate; } @keyframes firstanimation{ 0% {left:0px;} 100% {left:200px;} } @-webkit-keyframes firstanimation{ 0% {left:0px;} 100% {left:200px;} } @-moz-keyframes firstanimation{ 0% {left:0px;} 100% {left:200px;} } @-o-keyframes firstanimation{ 0% {left:0px;} 100% {left:200px;} } @-ms-keyframes firstanimation{ 0% {left:0px;} 100% {left:200px;} } @keyframes secondanimation{ 0% {top:0px;} 100% {top:200px;} } @-webkit-keyframes secondanimation{ 0% {top:0px;} 100% {top:200px;} } @-moz-keyframes secondanimation{ 0% {top:0px;} 100% {top:200px;} } @-o-keyframes secondanimation{ 0% {top:0px;} 100% {top:200px;} } @-ms-keyframes secondanimation{ 0% {top:0px;} 100% {top:200px;} } </style> </head> <body> <div></div> </body> </html>
The above code sets multiple sets of animation attributes for divs at one time, separated by commas.
Next Section