Maison > interface Web > tutoriel HTML > le corps du texte

CSS 3动画介绍_html/css_WEB-ITnose

WBOY
Libérer: 2016-06-24 11:53:04
original
851 Les gens l'ont consulté

原文:A Beginner’s Introduction to CSS Animation

译文:一个初学者对CSS动画的介绍

译者:dwqs

现在,越来越多的网站使用了动画,并且形式多样,如GIF、SVG、WebGL、背景视频等等。当在web中恰当使用动画时,它可以给网站注入活力和良好的交互性,给用户提供额外的一层反馈和体验。

在这篇文章,我将向你介绍CSS动画,随着浏览器对动画支持的改善,一种高效率的做事方式变得越来越流行。考虑到基础知识,我将快速建立一个从正方形变成圆形的动画案例。

先看看效果:http://jsfiddle.net/Web_Code/tchoafyh/embedded/result/

关于@keyframes和动画的介绍

CSS动画主要的组件是@keyframes,这个规则就是用来创建动画的。将@keyframes当作是时间轴的不同阶段,在其内部,你可以自定义时间轴的不同阶段,每个阶段有不同的CSS声明。

然后,为了使CSS动画生效,需要将@keyframes和一个选择器绑定。最后将会逐渐解析@keyframes内的全部代码,以阶段为划分,慢慢改变把最初的样式变成新的样式。

@keyframes元素

首先,定义动画的分隔。@keyframes的属性如下:

1、选择一个名字(在案例我选择tutsFade)

2、阶段划分:0%?100%,从0%到100%

3、CSS样式:你想要在每一个阶段用到的样式

例如:

@keyframe tutsFade{    0%{        opacity:1;    }    100%{        opacity:0;    }}
Copier après la connexion

 

          或者:

@keyframe tutsFade{     from{           opacity:1;     }     to{           opacity:0;     }}
Copier après la connexion

 

还有一种简写形式:

@keyframe tutsFade{     to{           opacity:0;     }}
Copier après la connexion

 

上述代码将对元素的透明度应用一个过渡效果:从1到0,三种方式最终的效果相同。

动画

Animation作为一个选择器去调用@keyframes。Animation有很多的属性:

1、animation-name:@keyframes的名字(例如tutsFade)

2、animation-duration:动画持续的时间

3、animation-timing-function:设置动画的速度特效,可以选择linear/ease-in/ease/ease-out/ease-in-out/cubic-bezier

4、animation-delay:动画开始之前的时间延迟

5、animation-iteration-count:动画循环的次数

6、animation-direction:规定动画是否反向轮播,normal是默认值,正常播放;alternate表示动画反向轮播

7、animation-fill-mode:规定动画在播放之前或之后,其动画效果是否可见(none/forwards/backwards/both)

    例如:

.element {  animation-name: tutsFade;  animation-duration: 4s;  animation-delay: 1s;  animation-iteration-count: infinite;  animation-timing-function: linear;  animation-direction: alternate;}
Copier après la connexion

 

            简写:

.element {  animation: tutsFade 4s 1s infinite linear alternate;}
Copier après la connexion

     添加私有前缀

            需要添加特定浏览器的私有前缀以确保最好的浏览器支持:chrome&Safari:-webkit-;Firefox:-moz-;Opera:-o-;IE:-ms-

            修改如下:

.element {    -webkit-animation: tutsFade 4s 1s infinite linear alternate;    -moz-animation: tutsFade 4s 1s infinite linear alternate;    -ms-animation: tutsFade 4s 1s infinite linear alternate;    -o-animation: tutsFade 4s 1s infinite linear alternate;    animation: tutsFade 4s 1s infinite linear alternate;}
Copier après la connexion

 

          @keyframes也一样

@-webkit-keyframes tutsFade { /* your style */ }@-moz-keyframes tutsFade { /* your style */ }@-ms-keyframes tutsFade { /* your style */ }@-o-keyframes tutsFade { /* your style */ }@keyframes tutsFade { /* your style */ }
Copier après la connexion

 

为了得到更多浏览器供应商的私有前缀,你可以去http://css3please.com/,查找,上面提供了非常丰富的资源。

多动画

可以添加多个动画,各个动画之间用逗号分隔。

.element {  animation: tutsFade 4s 1s infinite linear alternate, tutsRotate 4s 1s infinite linear alternate;}@keyframes tutsFade {  to {    opacity: 0;  }}@keyframes tutsRotate {  to {    transform: rotate(180deg);  }}
Copier après la connexion

 

     方形到圆形的动画教程

             利用上面的规则,我将创建一个简单的图形动画。总共会有5个阶段,并且在每个阶段都会对元素定义不同的Border-radius,rotation和background-color。

            1、基本元素

div {  width: 200px;  height: 200px;  background-color: coral;}
Copier après la connexion

 

2、声明Keyframes

创建一个名为square-to-circle的keyframe元素,包含5个阶段

@keyframes square-to-circle {  0%  {    border-radius:0 0 0 0;    background:coral;    transform:rotate(0deg);  }  25%  {    border-radius:50% 0 0 0;    background:darksalmon;    transform:rotate(45deg);  }  50%  {    border-radius:50% 50% 0 0;    background:indianred;    transform:rotate(90deg);  }  75%  {    border-radius:50% 50% 50% 0;    background:lightcoral;    transform:rotate(135deg);  }  100% {     border-radius:50%;    background:darksalmon;    transform:rotate(180deg);  }}
Copier après la connexion

3、应用动画

将定义的动画应用之前的div

div {  width: 200px;  height: 200px;  background-color: coral;  animation: square-to-circle 2s 1s infinite alternate; }
Copier après la connexion

 

4、使用时间函数和添加私有前缀

最后要添加的一个动画属性是animation-timing-function,它对动画元素的速度、加速和减速进行定义。一个类似的工具是:CSS Easing Animation Tool,可以使用它来计算时间函数。

在案例中,我给动画添加了一个cubic-bezier函数。

div {  width: 200px;  height: 200px;  background-color: coral;  animation: square-to-circle 2s 1s infinite cubic-bezier(1,.015,.295,1.225) alternate; }
Copier après la connexion

 

为了保证最好的浏览器支持,还必须添加私有前缀(没有添加前缀的代码如下)

div {  width: 200px;  height: 200px;  background-color: coral;  animation: square-to-circle 2s .5s infinite cubic-bezier(1,.015,.295,1.225) alternate;} @keyframes square-to-circle {  0%  {    border-radius:0 0 0 0;    background:coral;    transform:rotate(0deg);  }  25%  {    border-radius:50% 0 0 0;    background:darksalmon;    transform:rotate(45deg);  }  50%  {    border-radius:50% 50% 0 0;    background:indianred;    transform:rotate(90deg);  }  75%  {    border-radius:50% 50% 50% 0;    background:lightcoral;    transform:rotate(135deg);  }  100% {     border-radius:50%;    background:darksalmon;    transform:rotate(180deg);  }}
Copier après la connexion

 

这个在FireFox显示会有点异常,为了在FireFox有绝佳的显示效果,可以给div添加如下样式

outline: 1px solid transparent;
Copier après la connexion
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!