css中animation和@keyframes 动画
Animation
使用简写属性,将动画与 div 元素绑定:
- div
- {
- animation:mymove 5s infinite;
- -webkit-animation:mymove 5s infinite; /* Safari 和 Chrome */
- }
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" |
语法
- 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 元素匀速向下移动:
- @keyframes mymove
- {
- from {top:0px;}
- to {top:200px;}
- }
- @-moz-keyframes mymove /* Firefox */
- {
- from {top:0px;}
- to {top:200px;}
- }
- @-webkit-keyframes mymove /* Safari 和 Chrome */
- {
- from {top:0px;}
- to {top:200px;}
- }
- @-o-keyframes mymove /* Opera */
- {
- from {top:0px;}
- to {top:200px;}
- }
通过 @keyframes 规则,您能够创建动画。
创建动画的原理是,将一套 CSS 样式逐渐变化为另一套样式。
在动画过程中,您能够多次改变这套 CSS 样式。
以百分比来规定改变发生的时间,或者通过关键词 "from" 和 "to",等价于 0% 和 100%。
0% 是动画的开始时间,100% 动画的结束时间。
为了获得最佳的浏览器支持,您应该始终定义 0% 和 100% 选择器。
注释:请使用动画属性来控制动画的外观,同时将动画与选择器绑定。
- @keyframes animationname {keyframes-selector {css-styles;}}
值 | 描述 |
---|---|
animationname | 必需。定义动画的名称。 |
keyframes-selector |
必需。动画时长的百分比。 合法的值:
|
css-styles |
必需。一个或多个合法的 CSS 样式属性。 |
实例 1
在一个动画中添加多个 keyframe 选择器:
- @keyframes mymove
- {
- 0% {top:0px;}
- 25% {top:200px;}
- 50% {top:100px;}
- 75% {top:200px;}
- 100% {top:0px;}
- }
- @-moz-keyframes mymove /* Firefox */
- {
- 0% {top:0px;}
- 25% {top:200px;}
- 50% {top:100px;}
- 75% {top:200px;}
- 100% {top:0px;}
- }
- @-webkit-keyframes mymove /* Safari 和 Chrome */
- {
- 0% {top:0px;}
- 25% {top:200px;}
- 50% {top:100px;}
- 75% {top:200px;}
- 100% {top:0px;}
- }
- @-o-keyframes mymove /* Opera */
- {
- 0% {top:0px;}
- 25% {top:200px;}
- 50% {top:100px;}
- 75% {top:200px;}
- 100% {top:0px;}
- }
实例 2
在一个动画中改变多个 CSS 样式:
- @keyframes mymove
- {
- 0% {top:0px; background:red; width:100px;}
- 100% {top:200px; background:yellow; width:300px;}
- }
- @-moz-keyframes mymove /* Firefox */
- {
- 0% {top:0px; background:red; width:100px;}
- 100% {top:200px; background:yellow; width:300px;}
- }
- @-webkit-keyframes mymove /* Safari 和 Chrome */
- {
- 0% {top:0px; background:red; width:100px;}
- 100% {top:200px; background:yellow; width:300px;}
- }
- @-o-keyframes mymove /* Opera */
- {
- 0% {top:0px; background:red; width:100px;}
- 100% {top:200px; background:yellow; width:300px;}
- }
实例 3
带有多个 CSS 样式的多个 keyframe 选择器:
- @keyframes mymove
- {
- 0% {top:0px; left:0px; background:red;}
- 25% {top:0px; left:100px; background:blue;}
- 50% {top:100px; left:100px; background:yellow;}
- 75% {top:100px; left:0px; background:green;}
- 100% {top:0px; left:0px; background:red;}
- }
- @-moz-keyframes mymove /* Firefox */
- {
- 0% {top:0px; left:0px; background:red;}
- 25% {top:0px; left:100px; background:blue;}
- 50% {top:100px; left:100px; background:yellow;}
- 75% {top:100px; left:0px; background:green;}
- 100% {top:0px; left:0px; background:red;}
- }
- @-webkit-keyframes mymove /* Safari and Chrome */
- {
- 0% {top:0px; left:0px; background:red;}
- 25% {top:0px; left:100px; background:blue;}
- 50% {top:100px; left:100px; background:yellow;}
- 75% {top:100px; left:0px; background:green;}
- 100% {top:0px; left:0px; background:red;}
- }
- @-o-keyframes mymove /* Opera */
- {
- 0% {top:0px; left:0px; background:red;}
- 25% {top:0px; left:100px; background:blue;}
- 50% {top:100px; left:100px; background:yellow;}
- 75% {top:100px; left:0px; background:green;}
- 100% {top:0px; left:0px; background:red;}
- }
实例
- @keyframes myfirst
- {
- from {background: red;}
- to {background: yellow;}
- }
- @-moz-keyframes myfirst /* Firefox */
- {
- from {background: red;}
- to {background: yellow;}
- }
- @-webkit-keyframes myfirst /* Safari 和 Chrome */
- {
- from {background: red;}
- to {background: yellow;}
- }
- @-o-keyframes myfirst /* Opera */
- {
- from {background: red;}
- to {background: yellow;}
- }
CSS3 动画
当您在 @keyframes 中创建动画时,请把它捆绑到某个选择器,否则不会产生动画效果。
通过规定至少以下两项 CSS3 动画属性,即可将动画绑定到选择器:
- 规定动画的名称
- 规定动画的时长
实例
把 "myfirst" 动画捆绑到 div 元素,时长:5 秒:
- div
- {
- animation: myfirst 5s;
- -moz-animation: myfirst 5s; /* Firefox */
- -webkit-animation: myfirst 5s; /* Safari 和 Chrome */
- -o-animation: myfirst 5s; /* Opera */
- }
- 注释:您必须定义动画的名称和时长。如果忽略时长,则动画不会允许,因为默认值是 0。
实例
当动画为 25% 及 50% 时改变背景色,然后当动画 100% 完成时再次改变:
- @keyframes myfirst
- {
- 0% {background: red;}
- 25% {background: yellow;}
- 50% {background: blue;}
- 100% {background: green;}
- }
- @-moz-keyframes myfirst /* Firefox */
- {
- 0% {background: red;}
- 25% {background: yellow;}
- 50% {background: blue;}
- 100% {background: green;}
- }
- @-webkit-keyframes myfirst /* Safari 和 Chrome */
- {
- 0% {background: red;}
- 25% {background: yellow;}
- 50% {background: blue;}
- 100% {background: green;}
- }
- @-o-keyframes myfirst /* Opera */
- {
- 0% {background: red;}
- 25% {background: yellow;}
- 50% {background: blue;}
- 100% {background: green;}
- }
实例
改变背景色和位置:
- @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;}
- }
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 的动画,其中设置了所有动画属性:
- div
- {
- animation-name: myfirst;
- animation-duration: 5s;
- animation-timing-function: linear;
- animation-delay: 2s;
- animation-iteration-count: infinite;
- animation-direction: alternate;
- animation-play-state: running;
- /* Firefox: */
- -moz-animation-name: myfirst;
- -moz-animation-duration: 5s;
- -moz-animation-timing-function: linear;
- -moz-animation-delay: 2s;
- -moz-animation-iteration-count: infinite;
- -moz-animation-direction: alternate;
- -moz-animation-play-state: running;
- /* Safari 和 Chrome: */
- -webkit-animation-name: myfirst;
- -webkit-animation-duration: 5s;
- -webkit-animation-timing-function: linear;
- -webkit-animation-delay: 2s;
- -webkit-animation-iteration-count: infinite;
- -webkit-animation-direction: alternate;
- -webkit-animation-play-state: running;
- /* Opera: */
- -o-animation-name: myfirst;
- -o-animation-duration: 5s;
- -o-animation-timing-function: linear;
- -o-animation-delay: 2s;
- -o-animation-iteration-count: infinite;
- -o-animation-direction: alternate;
- -o-animation-play-state: running;
- }
实例
与上面的动画相同,但是使用了简写的动画 animation 属性:
- div
- {
- animation: myfirst 5s linear 2s infinite alternate;
- /* Firefox: */
- -moz-animation: myfirst 5s linear 2s infinite alternate;
- /* Safari 和 Chrome: */
- -webkit-animation: myfirst 5s linear 2s infinite alternate;
- /* Opera: */
- -o-animation: myfirst 5s linear 2s infinite alternate;
- }
css中animation和@keyframes 动画的更多相关文章
- CSS3 动画 animation和@keyframes
CSS3 @keyframes 规则 如需在 CSS3 中创建动画,您需要学习 @keyframes 规则. @keyframes 规则用于创建动画.在 @keyframes 中规定某项 CSS 样式 ...
- css3中的transform、transition、translate、animation(@keyframes)的区别
一.前言 在CSS中,我们经常会使用到transform.transition.translate.animation(@keyframes)这些长得相似,又不好区分的属性(值).每当需要使用它们,都 ...
- CSS中的动画
1.transition 在CSS3中,可以通过transition为元素从一种样式变换为另外一种样式的过程添加效果. transition为简写属性,用于在一个属性中设置四个过渡属性,分别是: tr ...
- Css中的变形及过渡动画
在css3的标准中新增加了变形样式,这些样式使得网页中各元素的位置形状的变换变得更加容易.其语法如下: transform:none | <transform-function>+ 其中对 ...
- CSS中的一下小技巧2之CSS3动画勾选运用
使用CSS3实现动画勾选 相信大家在项目中会经常遇到这种需求:勾选框.现在用CSS3来实现一个动画勾选,只需要一个标签即可完成: 这次需要用到CSS中伪类 after,这个小技巧也是很容易忘记的,所以 ...
- 第100天:CSS3中animation动画详解
CSS3属性中有关于制作动画的三个属性:Transform,Transition,Animation: 一.Animation定义动画 CSS3的Animation是由“keyframes”这个属性来 ...
- 示例:WPF中自定义StoryBoarService在代码中封装StoryBoard、Animation用于简化动画编写
原文:示例:WPF中自定义StoryBoarService在代码中封装StoryBoard.Animation用于简化动画编写 一.目的:通过对StoryBoard和Animation的封装来简化动画 ...
- android中Animation动画的连续播放与播放完毕后停留在最后的状态
我们做安卓应用的苦逼程序员们常常会需要用到Animation也就是动画.比如做地图功能的时候.我们在手机旋转时需要根据手机重力感应来调整地图的角度,让它上面的“北”一直指向地球的北面...好多人做动画 ...
- CSS 技巧一则 -- 在 CSS 中使用三角函数绘制曲线图形及展示动画
最近一直在使用 css-doodle 实现一些 CSS 效果. css-doodle 是一个基于 Web-Component 的库.允许我们快速的创建基于 CSS Grid 布局的页面,以实现各种 C ...
随机推荐
- [go]结构体/接口
接口惯用操作: 结构体构造方法返回接口类型 //定义服务器接口 type IServer interface{ Start() Stop() Serve() } type Server struct ...
- [Ubuntu]18安装百度网盘
1.下载客户端 下载地址: 选择linux版本,我选择的是deb格式,下载就可以了. 2.安装 进入下载目录,点击右键,选择在终端打开. 之后输入 以下代码愉快的安装就好了 注意:dpkg后面跟的文 ...
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
笔记 5.PageHelper分页插件使用 简介:讲解开源组件,mybaits分页插件的使用 1.引入依赖 <!-- 分页插件依赖 --> ...
- Qt编写自定义控件19-图片背景时钟
前言 图片背景时钟控件,是全套控件(目前共145个)中唯一的几个贴图的控件,这个背景要是不贴图,会画到猝死,必须用美工做好的图贴图作为背景,此控件以前学C#的时候写过,后面在写Qt控件的过程中把他移植 ...
- 科大讯飞sdk语音合成工具类
注:主要是dll文件的配置 A:Java SDK 使用了 JNI 形式,在初始化 SDK 时,SDK 将加载共享库(Windows下为msc32.dll或msc64.dll文件,Linux下libms ...
- 《A Survey on Transfer Learning》迁移学习研究综述 翻译
迁移学习研究综述 Sinno Jialin Pan and Qiang Yang,Fellow, IEEE 摘要: 在许多机器学习和数据挖掘算法中,一个重要的假设就是目前的训练数据和将来的训练数据 ...
- Git(4):远程仓库
添加\连接远程库 目前我们使用到的 Git 命令都是在本地执行,如果你想通过 Git 分享你的代码或者与其他开发人员合作. 你就需要将数据放到一台其他开发人员能够连接的服务器上. 远程仓库可以是Git ...
- 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 ...
- xiaopiu产品原型设计与团队实时协作平台
PRD文档创作 全新的文档创作模式,让交互原型与产品文档完美结合: 四大专业模板,满足多场景使用,快速输出专业规范的文档 PRD文档搜索 更专业.更精准的PRD文档垂直搜索服务,包含功能流程.协议条款 ...
- 【笔记】Docker部署Odoo
一,制作一个自己的odoo镜像odoo:yto 1,下载一个odoo10的镜像 docker pull odoo:10.0 2,按照自己的意愿修改里面的内容 docker run -it -u roo ...