登录  /  注册

对CSS3中动画(animation)的实例详解

零下一度
发布: 2017-06-30 10:11:37
原创
1713人浏览过

一:动画(animation)的参数详解

由于上面用到了animation动画,这里详细介绍下这个animation的参数。

简介

CSS动画(Animations)简单说就是在一段固定的动画时间内暗中在某一频率内改变其CSS某个或某些值,从而达到视觉上的转换动画效果。Animations的很多方面都是可以控制的,包括动画运行时间,开始值和结束值,还有动画的暂停和延迟其开始时间等。

语法

<single-animation> = <single-animation-name> || <time> || <single-animation-timing-function> || <time> || <single-animation-iteration-count> || <single-animation-direction> || <single-animation-fill-mode> || <single-animation-play-state></single-animation-play-state></single-animation-fill-mode></single-animation-direction></single-animation-iteration-count></time></single-animation-timing-function></time></single-animation-name></single-animation>

:检索或设置对象所应用的动画名称
:检索或设置对象动画的持续时间
:检索或设置对象动画的过渡类型
:检索或设置对象动画延迟的时间
:检索或设置对象动画的循环次数
:检索或设置对象动画在循环中是否反向运动
:检索或设置对象动画时间之外的状态
:检索或设置对象动画的状态。w3c正考虑是否将该属性移除,因为动画的状态可以通过其它的方式实现,比如重设样式

animation

所有动画属性的简写属性,除了 animation-play-state 属性。

animation-name

规定 @keyframes 动画的名称。就是@keyframes后面跟着的动画名称。

animation-duration

规定动画完成一个周期所花费的秒或毫秒。默认是 0。

animation-timing-function

规定动画的速度曲线。默认是 "ease"。

常见的动画速度参数:

  1. linear:线性过渡。等同于贝塞尔曲线(0.0, 0.0, 1.0, 1.0)

  2. ease:平滑过渡。等同于贝塞尔曲线(0.25, 0.1, 0.25, 1.0)

  3. ease-in:由慢到快。等同于贝塞尔曲线(0.42, 0, 1.0, 1.0)

  4. ease-out:由快到慢。等同于贝塞尔曲线(0, 0, 0.58, 1.0)

  5. ease-in-out:由慢到快再到慢。等同于贝塞尔曲线(0.42, 0, 0.58, 1.0)

  6. step-start:等同于 steps(1, start)

  7. step-end:等同于 steps(1, end)

  8. steps([, [ start | end ] ]?):接受两个参数的步进函数。第一个参数必须为正整数,指定函数的步数。第二个参数取值可以是start或end,指定每一步的值发生变化的时间点。第二个参数是可选的,默认值为end。

  9. cubic-bezier(, , , ):特定的贝塞尔曲线类型,4个数值需在[0, 1]区间内

animation-delay

规定动画何时开始。默认是 0。也即是指动画延时执行时间。

animation-iteration-count

规定动画被播放的次数。默认是 1。当然,我们可以设置2次,3次,依次递推。还有个无线循环关键字infinite,也即是反复循环播放动画。

animation-direction

规定动画是否在下一周期逆向地播放。默认是 "normal"。当然还有下列值:

  1. reverse反方向运行

  2. alternate动画先正常运行再反方向运行,并持续交替运行

  3. alternate-reverse动画先反运行再正方向运行,并持续交替运行

animation-fill-mode

规定对象动画时间之外的状态。

  1. none默认值。不设置对象动画之外的状态

  2. forwards设置对象状态为动画结束时的状态

  3. backwards设置对象状态为动画开始时的状态

  4. both设置对象状态为动画结束或开始的状态,动画开始之前是"from"或"0%"关键帧;动画完成之后是"to"或"100%"关键帧状态。

animation-play-state

规定动画是否正在运行或暂停。默认是 "running"。还有个值paused:暂停。

二:animation动画实例

实例一使用from to

div{width:100px;height:100px;background:red;position:relative;animation:mymove 5s infinite;-moz-animation:mymove 5s infinite; /*Firefox*/-webkit-animation:mymove 5s infinite; /*Safari and Chrome*/}@keyframes mymove{from {left:0px;}to {left:200px;}}
@-moz-keyframes mymove { /*Firefox*/from {left:0px;}to {left:200px;}}
@-webkit-keyframes mymove{ /*Safari and Chrome*/from {left:0px;}to {left:200px;}}
登录后复制

实例二使用百分比:

@keyframes myfirst{0%   {background: red; left:0px; top:0px;}25%  {background: yellow; left:200px; top:0px;}50%  {background: blue; left:200px; top:200px;}75%  {background: green; left:0px; top:200px;}100% {background: red; left:0px; top:0px;}}

@-moz-keyframes myfirst{ /* Firefox */0%   {background: red; left:0px; top:0px;}25%  {background: yellow; left:200px; top:0px;}50%  {background: blue; left:200px; top:200px;}75%  {background: green; left:0px; top:200px;}100% {background: red; left:0px; top:0px;}}

@-webkit-keyframes myfirst{ /* Safari 和 Chrome */0%   {background: red; left:0px; top:0px;}25%  {background: yellow; left:200px; top:0px;}50%  {background: blue; left:200px; top:200px;}75%  {background: green; left:0px; top:200px;}100% {background: red; left:0px; top:0px;}}

@-o-keyframes myfirst {/* Opera */0%   {background: red; left:0px; top:0px;}25%  {background: yellow; left:200px; top:0px;}50%  {background: blue; left:200px; top:200px;}75%  {background: green; left:0px; top:200px;}100% {background: red; left:0px; top:0px;}}
登录后复制

实例三,利用js+Transform和Animation实现3D动画

示例地址:

只有webkit内核的浏览器才能看到相关3D动画效果。

实现效果如图所示:

css代码:

body {font-family: 'Lucida Grande', Verdana, Arial;font-size: 12px;
      }  #stage {margin: 150px auto;width: 600px;height: 400px;-webkit-perspective: 800;
      }  #rotate {margin: 0 auto;width: 600px;height: 400px;-webkit-transform-style: preserve-3d;-webkit-animation-name: x-spin;-webkit-animation-duration: 7s;-webkit-animation-iteration-count: infinite;-webkit-animation-timing-function: linear;
      }  .ring {margin: 0 auto;height: 110px;width: 600px;-webkit-transform-style: preserve-3d;-webkit-animation-iteration-count: infinite;-webkit-animation-timing-function: linear;
      }  
      .ring &gt; :nth-child(odd) {background-color: #995C7F;
      }  .ring &gt; :nth-child(even) {background-color: #835A99;
      }  .poster {position: absolute;left: 250px;width: 100px;height: 100px;opacity: 0.7;color: rgba(0,0,0,0.9);-webkit-border-radius: 10px;
      }  
      .poster &gt; p {font-family: 'Georgia', serif;font-size: 36px;font-weight: bold;text-align: center;margin-top: 28px;
      }  #ring-1 {-webkit-animation-name: y-spin;-webkit-animation-duration: 5s;
      }  #ring-2 {-webkit-animation-name: back-y-spin;-webkit-animation-duration: 4s;
      }  #ring-3 {-webkit-animation-name: y-spin;-webkit-animation-duration: 3s;
      }  @-webkit-keyframes x-spin {0%    { -webkit-transform: rotateX(0deg); }50%   { -webkit-transform: rotateX(180deg); }100%  { -webkit-transform: rotateX(360deg); }  }

      @-webkit-keyframes y-spin {0%    { -webkit-transform: rotateY(0deg); }50%   { -webkit-transform: rotateY(180deg); }100%  { -webkit-transform: rotateY(360deg); }  }

      @-webkit-keyframes back-y-spin {0%    { -webkit-transform: rotateY(360deg); }50%   { -webkit-transform: rotateY(180deg); }100%  { -webkit-transform: rotateY(0deg); }  }
登录后复制

html代码:

<div>
  <div>
<div></div>
<div></div>
<div></div>
  </div>
</div>
登录后复制

js代码:

const POSTERS_PER_ROW = 12;
const RING_RADIUS = 200;function setup_posters (row){var posterAngle = 360 / POSTERS_PER_ROW;for (var i = 0; i 
登录后复制

以上就是对CSS3中动画(animation)的实例详解的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号