Animation

使用简写属性,将动画与 div 元素绑定:

  1. div
  2. {
  3. animation:mymove 5s infinite;
  4. -webkit-animation:mymove 5s infinite; /* Safari 和 Chrome */
  5. }
  6.  

Internet Explorer 10、Firefox 以及 Opera 支持 animation 属性。

Safari 和 Chrome 支持替代的 -webkit-animation 属性。

注释:Internet Explorer 9 以及更早的版本不支持 animation 属性。

定义和用法

animation 属性是一个简写属性,用于设置六个动画属性:

  • animation-name
  • animation-duration
  • animation-timing-function
  • animation-delay
  • animation-iteration-count
  • animation-direction

注释:请始终规定 animation-duration 属性,否则时长为 0,就不会播放动画了。

默认值: none 0 ease 0 1 normal
继承性: no
版本: CSS3
JavaScript 语法: object.style.animation="mymove 5s infinite"

语法

  1. animation: name duration timing-function delay iteration-count direction;
描述
animation-name 规定需要绑定到选择器的 keyframe 名称。。
animation-duration 规定完成动画所花费的时间,以秒或毫秒计。
animation-timing-function 规定动画的速度曲线。
animation-delay 规定在动画开始之前的延迟。
animation-iteration-count 规定动画应该播放的次数。
animation-direction 规定是否应该轮流反向播放动画。

@keyframes 规则

实例

使 div 元素匀速向下移动:

  1. @keyframes mymove
  2. {
  3. from {top:0px;}
  4. to {top:200px;}
  5. }
  6.  
  7. @-moz-keyframes mymove /* Firefox */
  8. {
  9. from {top:0px;}
  10. to {top:200px;}
  11. }
  12.  
  13. @-webkit-keyframes mymove /* Safari 和 Chrome */
  14. {
  15. from {top:0px;}
  16. to {top:200px;}
  17. }
  18.  
  19. @-o-keyframes mymove /* Opera */
  20. {
  21. from {top:0px;}
  22. to {top:200px;}
  23. }
  24.  

通过 @keyframes 规则,您能够创建动画。

创建动画的原理是,将一套 CSS 样式逐渐变化为另一套样式。

在动画过程中,您能够多次改变这套 CSS 样式。

以百分比来规定改变发生的时间,或者通过关键词 "from" 和 "to",等价于 0% 和 100%。

0% 是动画的开始时间,100% 动画的结束时间。

为了获得最佳的浏览器支持,您应该始终定义 0% 和 100% 选择器。

注释:请使用动画属性来控制动画的外观,同时将动画与选择器绑定。

  1. @keyframes animationname {keyframes-selector {css-styles;}}
描述
animationname 必需。定义动画的名称。
keyframes-selector

必需。动画时长的百分比。

合法的值:

  • 0-100%
  • from(与 0% 相同)
  • to(与 100% 相同)
css-styles

必需。一个或多个合法的 CSS 样式属性。

实例 1

在一个动画中添加多个 keyframe 选择器:

  1. @keyframes mymove
  2. {
  3. 0% {top:0px;}
  4. 25% {top:200px;}
  5. 50% {top:100px;}
  6. 75% {top:200px;}
  7. 100% {top:0px;}
  8. }
  9.  
  10. @-moz-keyframes mymove /* Firefox */
  11. {
  12. 0% {top:0px;}
  13. 25% {top:200px;}
  14. 50% {top:100px;}
  15. 75% {top:200px;}
  16. 100% {top:0px;}
  17. }
  18.  
  19. @-webkit-keyframes mymove /* Safari 和 Chrome */
  20. {
  21. 0% {top:0px;}
  22. 25% {top:200px;}
  23. 50% {top:100px;}
  24. 75% {top:200px;}
  25. 100% {top:0px;}
  26. }
  27.  
  28. @-o-keyframes mymove /* Opera */
  29. {
  30. 0% {top:0px;}
  31. 25% {top:200px;}
  32. 50% {top:100px;}
  33. 75% {top:200px;}
  34. 100% {top:0px;}
  35. }

实例 2

在一个动画中改变多个 CSS 样式:

  1. @keyframes mymove
  2. {
  3. 0% {top:0px; background:red; width:100px;}
  4. 100% {top:200px; background:yellow; width:300px;}
  5. }
  6.  
  7. @-moz-keyframes mymove /* Firefox */
  8. {
  9. 0% {top:0px; background:red; width:100px;}
  10. 100% {top:200px; background:yellow; width:300px;}
  11. }
  12.  
  13. @-webkit-keyframes mymove /* Safari 和 Chrome */
  14. {
  15. 0% {top:0px; background:red; width:100px;}
  16. 100% {top:200px; background:yellow; width:300px;}
  17. }
  18.  
  19. @-o-keyframes mymove /* Opera */
  20. {
  21. 0% {top:0px; background:red; width:100px;}
  22. 100% {top:200px; background:yellow; width:300px;}
  23. }

实例 3

带有多个 CSS 样式的多个 keyframe 选择器:

  1. @keyframes mymove
  2. {
  3. 0% {top:0px; left:0px; background:red;}
  4. 25% {top:0px; left:100px; background:blue;}
  5. 50% {top:100px; left:100px; background:yellow;}
  6. 75% {top:100px; left:0px; background:green;}
  7. 100% {top:0px; left:0px; background:red;}
  8. }
  9.  
  10. @-moz-keyframes mymove /* Firefox */
  11. {
  12. 0% {top:0px; left:0px; background:red;}
  13. 25% {top:0px; left:100px; background:blue;}
  14. 50% {top:100px; left:100px; background:yellow;}
  15. 75% {top:100px; left:0px; background:green;}
  16. 100% {top:0px; left:0px; background:red;}
  17. }
  18.  
  19. @-webkit-keyframes mymove /* Safari and Chrome */
  20. {
  21. 0% {top:0px; left:0px; background:red;}
  22. 25% {top:0px; left:100px; background:blue;}
  23. 50% {top:100px; left:100px; background:yellow;}
  24. 75% {top:100px; left:0px; background:green;}
  25. 100% {top:0px; left:0px; background:red;}
  26. }
  27.  
  28. @-o-keyframes mymove /* Opera */
  29. {
  30. 0% {top:0px; left:0px; background:red;}
  31. 25% {top:0px; left:100px; background:blue;}
  32. 50% {top:100px; left:100px; background:yellow;}
  33. 75% {top:100px; left:0px; background:green;}
  34. 100% {top:0px; left:0px; background:red;}
  35. }

实例

  1. @keyframes myfirst
  2. {
  3. from {background: red;}
  4. to {background: yellow;}
  5. }
  6.  
  7. @-moz-keyframes myfirst /* Firefox */
  8. {
  9. from {background: red;}
  10. to {background: yellow;}
  11. }
  12.  
  13. @-webkit-keyframes myfirst /* Safari 和 Chrome */
  14. {
  15. from {background: red;}
  16. to {background: yellow;}
  17. }
  18.  
  19. @-o-keyframes myfirst /* Opera */
  20. {
  21. from {background: red;}
  22. to {background: yellow;}
  23. }
  24.  

CSS3 动画

当您在 @keyframes 中创建动画时,请把它捆绑到某个选择器,否则不会产生动画效果。

通过规定至少以下两项 CSS3 动画属性,即可将动画绑定到选择器:

  • 规定动画的名称
  • 规定动画的时长

实例

把 "myfirst" 动画捆绑到 div 元素,时长:5 秒:

  1. div
  2. {
  3. animation: myfirst 5s;
  4. -moz-animation: myfirst 5s; /* Firefox */
  5. -webkit-animation: myfirst 5s; /* Safari 和 Chrome */
  6. -o-animation: myfirst 5s; /* Opera */
  7. }
  8.  
  9. 注释:您必须定义动画的名称和时长。如果忽略时长,则动画不会允许,因为默认值是 0

实例

当动画为 25% 及 50% 时改变背景色,然后当动画 100% 完成时再次改变:

  1. @keyframes myfirst
  2. {
  3. 0% {background: red;}
  4. 25% {background: yellow;}
  5. 50% {background: blue;}
  6. 100% {background: green;}
  7. }
  8.  
  9. @-moz-keyframes myfirst /* Firefox */
  10. {
  11. 0% {background: red;}
  12. 25% {background: yellow;}
  13. 50% {background: blue;}
  14. 100% {background: green;}
  15. }
  16.  
  17. @-webkit-keyframes myfirst /* Safari 和 Chrome */
  18. {
  19. 0% {background: red;}
  20. 25% {background: yellow;}
  21. 50% {background: blue;}
  22. 100% {background: green;}
  23. }
  24.  
  25. @-o-keyframes myfirst /* Opera */
  26. {
  27. 0% {background: red;}
  28. 25% {background: yellow;}
  29. 50% {background: blue;}
  30. 100% {background: green;}
  31. }
  32.  

实例

改变背景色和位置:

  1. @keyframes myfirst
  2. {
  3. 0% {background: red; left:0px; top:0px;}
  4. 25% {background: yellow; left:200px; top:0px;}
  5. 50% {background: blue; left:200px; top:200px;}
  6. 75% {background: green; left:0px; top:200px;}
  7. 100% {background: red; left:0px; top:0px;}
  8. }
  9.  
  10. @-moz-keyframes myfirst /* Firefox */
  11. {
  12. 0% {background: red; left:0px; top:0px;}
  13. 25% {background: yellow; left:200px; top:0px;}
  14. 50% {background: blue; left:200px; top:200px;}
  15. 75% {background: green; left:0px; top:200px;}
  16. 100% {background: red; left:0px; top:0px;}
  17. }
  18.  
  19. @-webkit-keyframes myfirst /* Safari 和 Chrome */
  20. {
  21. 0% {background: red; left:0px; top:0px;}
  22. 25% {background: yellow; left:200px; top:0px;}
  23. 50% {background: blue; left:200px; top:200px;}
  24. 75% {background: green; left:0px; top:200px;}
  25. 100% {background: red; left:0px; top:0px;}
  26. }
  27.  
  28. @-o-keyframes myfirst /* Opera */
  29. {
  30. 0% {background: red; left:0px; top:0px;}
  31. 25% {background: yellow; left:200px; top:0px;}
  32. 50% {background: blue; left:200px; top:200px;}
  33. 75% {background: green; left:0px; top:200px;}
  34. 100% {background: red; left:0px; top:0px;}
  35. }
  36.  

CSS3 动画属性

下面的表格列出了 @keyframes 规则和所有动画属性:

属性 描述 CSS
@keyframes 规定动画。 3
animation 所有动画属性的简写属性,除了 animation-play-state 属性。 3
animation-name 规定 @keyframes 动画的名称。 3
animation-duration 规定动画完成一个周期所花费的秒或毫秒。默认是 0。 3
animation-timing-function 规定动画的速度曲线。默认是 "ease"。 3
animation-delay 规定动画何时开始。默认是 0。 3
animation-iteration-count 规定动画被播放的次数。默认是 1。 3
animation-direction 规定动画是否在下一周期逆向地播放。默认是 "normal"。 3
animation-play-state 规定动画是否正在运行或暂停。默认是 "running"。 3
animation-fill-mode 规定对象动画时间之外的状态。 3

实例

运行名为 myfirst 的动画,其中设置了所有动画属性:

  1. div
  2. {
  3. animation-name: myfirst;
  4. animation-duration: 5s;
  5. animation-timing-function: linear;
  6. animation-delay: 2s;
  7. animation-iteration-count: infinite;
  8. animation-direction: alternate;
  9. animation-play-state: running;
  10. /* Firefox: */
  11. -moz-animation-name: myfirst;
  12. -moz-animation-duration: 5s;
  13. -moz-animation-timing-function: linear;
  14. -moz-animation-delay: 2s;
  15. -moz-animation-iteration-count: infinite;
  16. -moz-animation-direction: alternate;
  17. -moz-animation-play-state: running;
  18. /* Safari 和 Chrome: */
  19. -webkit-animation-name: myfirst;
  20. -webkit-animation-duration: 5s;
  21. -webkit-animation-timing-function: linear;
  22. -webkit-animation-delay: 2s;
  23. -webkit-animation-iteration-count: infinite;
  24. -webkit-animation-direction: alternate;
  25. -webkit-animation-play-state: running;
  26. /* Opera: */
  27. -o-animation-name: myfirst;
  28. -o-animation-duration: 5s;
  29. -o-animation-timing-function: linear;
  30. -o-animation-delay: 2s;
  31. -o-animation-iteration-count: infinite;
  32. -o-animation-direction: alternate;
  33. -o-animation-play-state: running;
  34. }

实例

与上面的动画相同,但是使用了简写的动画 animation 属性:

  1. div
  2. {
  3. animation: myfirst 5s linear 2s infinite alternate;
  4. /* Firefox: */
  5. -moz-animation: myfirst 5s linear 2s infinite alternate;
  6. /* Safari 和 Chrome: */
  7. -webkit-animation: myfirst 5s linear 2s infinite alternate;
  8. /* Opera: */
  9. -o-animation: myfirst 5s linear 2s infinite alternate;
  10. }

css中animation和@keyframes 动画的更多相关文章

  1. CSS3 动画 animation和@keyframes

    CSS3 @keyframes 规则 如需在 CSS3 中创建动画,您需要学习 @keyframes 规则. @keyframes 规则用于创建动画.在 @keyframes 中规定某项 CSS 样式 ...

  2. css3中的transform、transition、translate、animation(@keyframes)的区别

    一.前言 在CSS中,我们经常会使用到transform.transition.translate.animation(@keyframes)这些长得相似,又不好区分的属性(值).每当需要使用它们,都 ...

  3. CSS中的动画

    1.transition 在CSS3中,可以通过transition为元素从一种样式变换为另外一种样式的过程添加效果. transition为简写属性,用于在一个属性中设置四个过渡属性,分别是: tr ...

  4. Css中的变形及过渡动画

    在css3的标准中新增加了变形样式,这些样式使得网页中各元素的位置形状的变换变得更加容易.其语法如下: transform:none | <transform-function>+ 其中对 ...

  5. CSS中的一下小技巧2之CSS3动画勾选运用

    使用CSS3实现动画勾选 相信大家在项目中会经常遇到这种需求:勾选框.现在用CSS3来实现一个动画勾选,只需要一个标签即可完成: 这次需要用到CSS中伪类 after,这个小技巧也是很容易忘记的,所以 ...

  6. 第100天:CSS3中animation动画详解

    CSS3属性中有关于制作动画的三个属性:Transform,Transition,Animation: 一.Animation定义动画 CSS3的Animation是由“keyframes”这个属性来 ...

  7. 示例:WPF中自定义StoryBoarService在代码中封装StoryBoard、Animation用于简化动画编写

    原文:示例:WPF中自定义StoryBoarService在代码中封装StoryBoard.Animation用于简化动画编写 一.目的:通过对StoryBoard和Animation的封装来简化动画 ...

  8. android中Animation动画的连续播放与播放完毕后停留在最后的状态

    我们做安卓应用的苦逼程序员们常常会需要用到Animation也就是动画.比如做地图功能的时候.我们在手机旋转时需要根据手机重力感应来调整地图的角度,让它上面的“北”一直指向地球的北面...好多人做动画 ...

  9. CSS 技巧一则 -- 在 CSS 中使用三角函数绘制曲线图形及展示动画

    最近一直在使用 css-doodle 实现一些 CSS 效果. css-doodle 是一个基于 Web-Component 的库.允许我们快速的创建基于 CSS Grid 布局的页面,以实现各种 C ...

随机推荐

  1. [go]结构体/接口

    接口惯用操作: 结构体构造方法返回接口类型 //定义服务器接口 type IServer interface{ Start() Stop() Serve() } type Server struct ...

  2. [Ubuntu]18安装百度网盘

     1.下载客户端 下载地址: 选择linux版本,我选择的是deb格式,下载就可以了. 2.安装 进入下载目录,点击右键,选择在终端打开. 之后输入 以下代码愉快的安装就好了 注意:dpkg后面跟的文 ...

  3. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用

    笔记 5.PageHelper分页插件使用     简介:讲解开源组件,mybaits分页插件的使用 1.引入依赖             <!-- 分页插件依赖 -->          ...

  4. Qt编写自定义控件19-图片背景时钟

    前言 图片背景时钟控件,是全套控件(目前共145个)中唯一的几个贴图的控件,这个背景要是不贴图,会画到猝死,必须用美工做好的图贴图作为背景,此控件以前学C#的时候写过,后面在写Qt控件的过程中把他移植 ...

  5. 科大讯飞sdk语音合成工具类

    注:主要是dll文件的配置 A:Java SDK 使用了 JNI 形式,在初始化 SDK 时,SDK 将加载共享库(Windows下为msc32.dll或msc64.dll文件,Linux下libms ...

  6. 《A Survey on Transfer Learning》迁移学习研究综述 翻译

    迁移学习研究综述 Sinno Jialin Pan and Qiang Yang,Fellow, IEEE 摘要:   在许多机器学习和数据挖掘算法中,一个重要的假设就是目前的训练数据和将来的训练数据 ...

  7. Git(4):远程仓库

    添加\连接远程库 目前我们使用到的 Git 命令都是在本地执行,如果你想通过 Git 分享你的代码或者与其他开发人员合作. 你就需要将数据放到一台其他开发人员能够连接的服务器上. 远程仓库可以是Git ...

  8. How to remove duplicate lines in a large text file?

    How would you remove duplicate lines from a file that is  much too large to fit in memory? The dupli ...

  9. xiaopiu产品原型设计与团队实时协作平台

    PRD文档创作 全新的文档创作模式,让交互原型与产品文档完美结合: 四大专业模板,满足多场景使用,快速输出专业规范的文档 PRD文档搜索 更专业.更精准的PRD文档垂直搜索服务,包含功能流程.协议条款 ...

  10. 【笔记】Docker部署Odoo

    一,制作一个自己的odoo镜像odoo:yto 1,下载一个odoo10的镜像 docker pull odoo:10.0 2,按照自己的意愿修改里面的内容 docker run -it -u roo ...