CSS3 Animation
There are three properties for animating in the CSS3 property: transform,transition,animation; We have finished studying together Transform and Transition, let's have some basic animation effect on the elements, I think it's enough for everyone to get excited for a while, today we're going to take advantage of this honeymoon. The third animated attribute animation's learning, from the animation literal meaning, we know is "animation" meaning. But the animation in CSS3 is different from the canvas drawing animation in HTML5, and animation only applies to DOM elements that already exist on the page, and he does not have the same animated effect as Flash and JavaScript and jquery. Because we use CSS3 's animation to make the animation we can save the complex js,jquery code (like I do not understand JS is a very happy thing), just a little bit of a disadvantage, we use animation can create some of the animation we want to effect, But a bit rough, if you want to make a better animation, I see you still use Flash or JS and so on. Although the animation made by animation is simple and rough, I think we can not reduce the enthusiasm of all of us for their study.
Before we begin to introduce animation, we need to understand a special thing, that is, "Keyframes", we call him "key frame", played flash friends may not be unfamiliar with this thing. Let's take a look at what this "Keyframes" is. Before we used transition to make a simple transition effect, we included the initial and final attributes, a starting action time and a continuation action time, and the change rate of the action, in fact these values are an intermediate value, if we want to control the finer, For example, I want to take the first time to perform what action, the second time period to perform what action (in Flash said, is the first frame I want to perform what action, the second frame I want to perform what action), so we use transition is difficult to achieve, at this time we also need such a "keyframe" to control. Then the CSS3 animation is the "keyframes" this property to achieve this effect. Let's take a look at KeyFrames first:
KeyFrames has its own grammar rules, his name is "@keyframes" beginning, followed by the "name of the animation" plus a pair of curly braces "{}", the parentheses are some of the different time-period style rules, a bit like our CSS style. For a style rule in a "@keyframes" is made up of a number of percentages, such as "0%" to "100%", we can create multiple percentages in this rule, we give each of the percentages to animate elements with different attributes, This allows elements to reach a changing effect, such as moving, changing element color, position, size, shape, and so on, but one thing to note is that we can use "fromt" "to" to represent where an animation starts and ends, meaning that the "from" is equivalent to "0%" and " To "equivalent to" 100% ", it is worth saying that" 0% "can not be the same as other attributes to the value of the percentage symbol omitted, we must add a percent sign here ("% ") if not added, we this keyframes is invalid, does not play any role. Because keyframes units accept only percent values.
KeyFrames can specify any order in which to determine the key position of animation animation changes. Its specific syntax rules are as follows:
Keyframes-rule: ' @keyframes ' IDENT ' {' keyframes-blocks '}; Keyframes-blocks: [keyframe-selectors block]*; Keyframe-selectors: [' From ' | ' To ' | PERCENTAGE] [', ' [' From ' | ' To ' | PERCENTAGE]]*;
I'm combining the grammar above.
@keyframes IDENT {from { properties:properties value; } Percentage { properties:properties value; } To { properties:properties value; } } Or all written in percent form: @keyframes IDENT { 0% { properties:properties value; } Percentage { properties:properties value; } 100% { properties:properties value; } }
Where ident is an animated name, you can take it, of course, semantically a little better, percentage is a percentage value, we can add a number of such percentages, properties for CSS property names, such as Left,background, etc., Value is the property value of the corresponding property. It is worth mentioning that our from and to respectively corresponds to 0% and 100%. We mentioned this earlier, too. So far the animation animation of only the WebKit kernel browser, so I need to add-webkit prefix above, it is said that FIREFOX5 can support CSS3 animation properties.
Let's take a look at an example of the website
@-webkit-keyframes ' wobble ' { 0% { margin-left:100px; Background:green; } 40% { margin-left:150px; Background:orange; } 60% { margin-left:75px; Background:blue; } 100% { margin-left:100px; background:red; } }
Here we define a "wobble" animation, his animation is from 0% to 100% end, also experienced a 40% and 60% two process, the above code specifically means: Wobble animation at 0% when the element is positioned to the left 100px position background color green , and then 40% when the element transitions to the left 150px position and the background color is orange,60% when the element transitions to the left 75px position, the background color is blue, the last 100% end animation position element back to the starting point left 100px, the background color into red. False setting we only give this animation a 10s execution time, then the status of each of his executions is as follows:
After the keyframes is defined, how do we need to invoke the animated "wobble" that we have just defined?
CSS3 animation are similar to transition properties, and they all change the attribute value of an element over time. Their main difference is that transition needs to trigger an event (hover event or click event) to change its CSS properties over time, and animation can change the property value of the element CSS explicitly over time without triggering any events. So as to achieve an animation effect. So that we can call animation's animated property directly in an element, and based on this, CSS3 's animation needs to have a definite animated property value, which is what we call a keyframes to define CSS property values at different times. Achieve the effect of the element at different time periods.
Let's take a look at how to call an element animation property
. demo1 { width:50px; height:50px; margin-left:100px; Background:blue; -webkit-animation-name: ' wobble ';/* animated property name, which is the animated name defined in front of us keyframes */ -webkit-animation-duration:10s;/* animation Duration */ -webkit-animation-timing-function:ease-in-out;/* animation frequency, and transition-timing-function is the same * /- webkit-animation-delay:2s;/* Animation Delay Time * /-webkit-animation-iteration-count:10;/* Define the loop data, infinite for infinite times * /- webkit-animation-direction:alternate;/* Define Animation Mode */ }
CSS animation animation effect will affect the element corresponding to the CSS value, throughout the animation process, the element changes the value of the property is completely controlled by the animation, after the animation will overwrite the previous property values. As the above example: because our demo just changed the background color and the left margin of the demo1 in different time periods, the default value is: Margin-left:100px;background:blue; But when we're doing animation 0%, Margin-left : 100px,background:green; When executed to 40%, the property becomes: margin-left:150px;background:orange; when executed to 60% Margin-left:75px;background : Blue; When the animation is executed to 100%: margin-left:100px;background:red, the animation will be completed, then margin-left and background two attribute values will be 100% when the main, he does not produce a superposition effect, It's just a one-time overwrite of the previous CSS property. Just like our usual CSS, the last occurrence of the right root is the largest. When the animation finishes, the style returns to the default effect.
We can see a picture from the website about the CSS3 of the animation on the property change process
From the above demo we can see that animation and transition have their own corresponding properties, then there are several main animation in the following: Animation-name;animation-duration; Animation-timing-function;animation-delay;animation-iteration-count;animation-direction;animation-play-state. Let's take a look at the use of these several attributes separately
First, Animation-name:
Grammar:
Animation-name:none | Ident[,none | ident]*;
Value Description:
Animation-name: is used to define the name of an animation, which has two values: Ident is the name of the animation created by keyframes, in other words, the ident here is consistent with the keyframes in ident, and if not, no animated effect will be achieved ; none is the default value, and no animation effect occurs when the values are none. In addition, we have this property as in the previous transition, we can attach a few animation to an element at the same time, we just need to use a comma "," separated.
Second, Animation-duration:
Grammar:
Animation-duration: <time>[,<time>]*
Value Description:
Animation-duration is used to specify the length of time that an element plays an animation, and the value:<time> is a number, in S (seconds). Its default value is "0". This property is the same as the transition-duration used in transition.
Third, Animation-timing-function:
Grammar:
Animation-timing-function:ease | Linear | ease-in | Ease-out | Ease-in-out | Cubic-bezier (<NUMBER>, <number>, <number>, <number>) [, ease | linear | ease-in | ease-out | ease- In-out | Cubic-bezier (<NUMBER>, <number>, <number>, <number>)]*
Value Description:
Animation-timing-function: Refers to the element according to the time of the advance to change the property value of the transformation rate, the simple point is the way the animation play. Like the transition-timing-function in transition, he has the following six ways of transformation: Ease;ease-in;ease-in-out;linear;cubic-bezier. The specific use of the method you can click here to see how the use of transition-timing-function.
Four, Animation-delay:
Grammar:
Animation-delay: <time>[,<time>]*
Value Description:
Animation-delay: is used to specify the element animation start time. The value is <time> as the value, in s (seconds), and the default value is 0. This property is the same as the Transition-delayy use method.
Wu, Animation-iteration-count
Grammar:
Animation-iteration-count:infinite | <number> [, Infinite | <number>]*
Value Description:
Animation-iteration-count is used to specify the number of cycles that an element plays an animation, which can take a value of <number> as a number, with a default value of "1" and a infinite loop for an infinite number of times.
Liu, Animation-direction
Grammar:
Animation-direction:normal | alternate [, Normal | alternate]*
Value Description:
Animation-direction is used to specify the direction in which an element animates, with only two values, the default is normal, and if set to normal, each cycle of the animation is played forward, and the other value is alternate, and his role is, The animation plays forward in an even number of times, and the odd number of times plays in the opposite direction.
Seven, Animation-play-state
Grammar:
animation-play-state:running | Paused [, running | paused]*
Value Description:
Animation-play-state is primarily used to control the playback state of element animations. There are mainly two values, running and paused where running is the default value. Their role is similar to our music player, you can stop the animation being played by paused, or you can replay the paused animation by running, our replay here is not necessarily from the beginning of the element animation play, but from the position you paused to start playing. In addition, if the animation is temporarily played, the style of the element will return to the most original setting state. This property is rarely supported by the kernel at this time, so just mention it slightly.
We introduce the syntax and values of each property in animation, so we can give the animation attribute a shorthand by combining the above contents:
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>]]*
As shown
Compatible browsers
I also briefly mentioned earlier, CSS3 's animation so far only support WebKit Kernel browser, because the first to propose this property is the safari company, it is said that firefox5.0+ will support animation.
So far, we have studied the theoretical knowledge of animation together, let's take a look at two instance making process to strengthen the practical ability of animation
Demo One: The button that glows color
Our demo is mainly by changing the elements of the keyframes in the Background;color;box-shadow three properties, to achieve a glow-changing button effect, we look at its implementation code
HTML Code:
<a href= "" class= "btn" > Glowing button</a>
CSS Code
/* Create a name for this button: Buttonlight, then set a different background,color in each time period to achieve the discoloration effect, change the Box-shadow to achieve the luminous effect * *
@-webkit-keyframes ' Buttonlight ' {from {Background:rgba (96, 203, 27,0.5); -webkit-box-shadow:0 0 5px Rgba (255, 255, 255, 0.3) inset, 0 0 3px Rgba (220, 120, 200, 0.5); color:red; } 25% {Background:rgba (196, 203, 27,0.8); -webkit-box-shadow:0 0 10px Rgba (255, 155, 255, 0.5) inset, 0 0 8px Rgba (120, 120, 200, 0.8); Color:blue; } 50% {Background:rgba (196, 203, 127,1); -webkit-box-shadow:0 0 5px Rgba (155, 255, 255, 0.3) inset, 0 0 3px Rgba (220, 120, 100, 1); Color:orange; } 75% {Background:rgba (196, 203, 27,0.8); -webkit-box-shadow:0 0 10px Rgba (255, 155, 255, 0.5) inset, 0 0 8px Rgba (120, 120, 200, 0.8); Color:black; } to {Background:rgba (96, 203, 27,0.5); -webkit-box-shadow:0 0 5px Rgba (255, 255, 255, 0.3) inset, 0 0 3px Rgba (220, 120, 200, 0.5); Color:green; }} a.btn the basic properties of the {/* button */background: #60cb1b; font-size:16px; padding:10px 15px; COlor: #fff; Text-align:center; Text-decoration:none; Font-weight:bold; text-shadow:0 -1px 1px rgba (0,0,0,0.3); -moz-border-radius:5px; -webkit-border-radius:5px; border-radius:5px; -moz-box-shadow:0 0 5px Rgba (255, 255, 255, 0.6) inset, 0 0 3px Rgba (220, 120, 200, 0.8); -webkit-box-shadow:0 0 5px Rgba (255, 255, 255, 0.6) inset, 0 0 3px Rgba (220, 120, 200, 0.8); box-shadow:0 0 5px Rgba (255, 255, 255, 0.6) inset, 0 0 3px Rgba (220, 120, 200, 0.8); /* Call the animation property to have the button animate when loading the page */-webkit-animation-name: "Buttonlight"; /* The animated name needs to match the name defined by the @keyframes */-webkit-animation-duration:5s;/* animation lasts longer */-webkit-animation-iteration-count: infinite;/* the number of times the animation loop plays */}
Effect:
Effect One
Change in effect two
To better see the effect of this demo, you can copy the above code to your page, and use Safari and chrome, you will find it very interesting, the whole button seems to be constantly breathing.
Demo two: square rotation into round
We this demo is through the transform rotate and Border-radius different values, a square image with the passage of time, slowly converted into a circular effect, the following we look at its specific implementation of the effect
HTML Code:
<a href= "#" class= "box" ></a> <span class= "Click-btn" >Click</span>
CSS Code:
/* Define square to round animation round*/
@-webkit-keyframes ' round ' {from{-webkit-transform:rotate (36deg); -webkit-border-radius:2px; } 10%{-webkit-transform:rotate (72deg); -webkit-border-radius:4px; } 20% {-webkit-transform:rotate (108deg); -webkit-border-radius:6px; } 30% {-webkit-transform:rotate (144deg); -webkit-border-radius:9px; } 40%{-webkit-transform:rotate (180deg); -webkit-border-radius:12px; } 50%{-webkit-transform:rotate (216deg); -webkit-border-radius:14px; } 60% {-webkit-transform:rotate (252deg); -webkit-border-radius:16px; } 70% {-webkit-transform:rotate (288deg); -webkit-border-radius:19px; } 80%{-webkit-transform:rotate (324deg); -webkit-border-radius:22px; } to {-webkit-transform:rotate (360deg); -webkit-border-radius:25px; }}/* Give square box A preliminary style */A.box {display:block; width:50px; height:50px; background:red; margin-bottom:20px;}/* Style of the round box, and apply animation*/A.round {-webkit-border-radius:25px here; -moz-border-radius:25px; border-radius:25px; Background:green; -webkit-animation-name: ' Round '; /* Animated name */-webkit-animation-duration:60s;/* Play time duration */-webkit-animation-timing-function:ease;/* animation Playback frequency */- webkit-animation-iteration-count:infinite;/* animation plays indent for infinite Times */}/*click button Effect */. click-btn {BACKGR Ound:rgba (125,220,80,0.8); -moz-border-radius:5px; -webkit-border-radius:5px; border-radius:5px; -webkit-box-shadow:inset 0 0 8px Rgba (255,255,255,0.8), 0 0 10px Rgba (10,255,120,0.3); -moz-box-shadow:inset 0 0 8px Rgba (255,255,255,0.8), 0 0 10px Rgba (10,255,120,0.3); Box-shadow:inset 0 0 8px Rgba (255,255,255,0.8), 0 0 10px Rgba (10,255,120,0.3); padding:5px 10px; Color: #369; font-size:16px; Font-weight:bold; Text-align:center; text-shadow:0 -1px 0 Rgba (0,0,0,0.5); CurSor:pointer; }
JQuery Code:
<script type= "Text/javascript" > $ (document). Ready (function () { $ (". Click-btn"). Click (function () { $ (this). Siblings (). AddClass ("Round") ;}); </script>
The box was not animated when we loaded it, and when we clicked the Click button We added a round class name to the original box, triggering a round action. Please see the effect:
Effect when button is not clicked (animation effect not triggered)
Press the Click button to start playing the animation
"Recommended"
1. CSS3 Free Video Tutorial
2. CSS3 Teaching Animation Production Learning
3. Detailed analysis of new features in CSS3
4. A detailed description of the new features of CSS3
5. Recommended 10 examples of CSS3 animations