教程集 www.jiaochengji.com
教程集 >  CSS教程  >  正文 CSS3中动画的一些使用方法介绍

CSS3中动画的一些使用方法介绍

发布时间:2019-10-31   编辑:jiaochengji.com
教程集为您提供CSS3中动画的一些使用方法介绍等资源,欢迎您收藏本站,我们将为您提供最新的CSS3中动画的一些使用方法介绍资源
CSS3中动画是一个非常常用并且简单实现的功能了,只从有了css3之后大家可以不用js或者flash来做动画了,有些简单的动画直接使用css即可完成,下面看个例子。

关于@keyframes和动画的介绍

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

然后,为了使CSS动画生效,需要将@keyframes和一个选择器绑定。最后将会逐渐解析@keyframes内的全部代码,以阶段为划分,慢慢改变把最初的样式变成新的样式。
@keyframes元素
首先,定义动画的分隔。@keyframes的属性如下:
1、选择一个名字(在案例我选择tutsFade)
2、阶段划分:0%–100%,从0%到100%
3、CSS样式:你想要在每一个阶段用到的样式
例如:
<pre id="codeSnippet">@keyframe tutsFade{ 0%{ opacity:1; } 100%{ opacity:0; } }</pre>
 
          或者:
<pre id="codeSnippet">@keyframe tutsFade{ from{ opacity:1; } to{ opacity:0; } }</pre>
 
还有一种简写形式:
<pre id="codeSnippet">@keyframe tutsFade{ to{ opacity:0; } }</pre>
 
上述代码将对元素的透明度应用一个过渡效果:从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
animation
4、animation-delay:动画开始之前的时间延迟
5、animation-iteration-count:动画循环的次数

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

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

animation2

例如:
.element {
  animation-name: tutsFade;
  animation-duration: 4s;
  animation-delay: 1s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  animation-direction: alternate;
}
 

            简写:
.element {
  animation: tutsFade 4s 1s infinite linear alternate;
}
     添加私有前缀
            需要添加特定浏览器的私有前缀以确保最好的浏览器支持: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;
}
 

          @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 */ }
 

为了得到更多浏览器供应商的私有前缀,你可以去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);
  }
}
 

     方形到圆形的动画教程
             利用上面的规则,我将创建一个简单的图形动画。总共会有5个阶段,并且在每个阶段都会对元素定义不同的Border-radius,rotation和background-color。
            1、基本元素
div {
  width: 200px;
  height: 200px;
  background-color: coral;
}
 

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);
  }
}
3、应用动画

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

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

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;
}
 

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

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);
  }
}
 

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

outline: 1px solid transparent;

您可能感兴趣的文章:
一个简单的CSS3动画样式
jQuery Animation实现CSS3动画示例介绍
css3中 transition 与 animation用法介绍
CSS3 Animations创建返回顶部的平滑动画
CSS3 animation动画属性使用例子
CSS3中Animation为同一个元素添加多个动画效果
CSS3中动画的一些使用方法介绍
jQuery实现CSS3动画效果的插件 jQuery Transit
6款新颖的jQuery和CSS3进度条插件推荐
HTML5必读书籍

[关闭]
~ ~