CSS3的transition和animation的用法实例介绍


Posted in HTML / CSS onAugust 20, 2014

transition

transition 属性是
transition-property,
transition-duration,
transition-timing-function,
transition-delay
的简称,用于设定一个元素的两个状态,不同的状态可以用伪类,比如:hover, :active 或者是通过 javascript 动态设定。IE10+支持。

所以 transition 的初始值为:

transition-delay: 0s;
transition-duration: 0s;
transition-property: all;
transition-timing-function: ease;

用法

div {
transition: <property> <duration> <timing-function> <delay>;
}

并且有事件可以监测 transition 结束

el.addEventListener("transitionend",updateTransition,true);

//in webkit
el.addEventListener("webkitTransitionEnd",updateTransition,true);

实例

HTML

复制代码
代码如下:

<!-- DEMO 1: Fade Block -->
<div id="fade">
move here !
</div>
<div id="nudge">
mouse on me
</div>
<div id="bounce">Place mouse on me i will bounce!</div>
<div id="spin">Place mouse on me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i will spin</div>
<div id="accordion" class="accordion">
<a href="#first">This is first tab</a>
<div id="first"><p>Lorem ipsum </p> </div>
<a href="#second">This is second tab</a>
<div id="second"><p>Lorem ipsum </p> </div>
<a href="#third">This is third tab</a>
<div id="third"><p>Lorem ipsum </p> </div>
</div>

CSS
复制代码
代码如下:

/*
DEMO 1: Fade Block
*/
div {
margin-bottom: 50px;
}
#fade {
/*opacity:1;
-webkit-transition: opacity 10s liner 10s;*/
position: relative;
transition-property: font-size;
transition-duration: 0.5s;
transition-delay: 0;
font-size: 14px;
}
#fade:hover {
font-size: 36px;
}

/* DEMO2 */
#nudge{
-webkit-transition-property: color,
background-color,padding-left;
-webkit-transition-duration: 500ms,500ms, 500ms;
}
#nudge:hover{
background-color: #efefef;
color: #333;
padding-left: 50px;
}
#bounce:hover {
-webkit-animation-name:bounce;
-webkit-animation-duration:1s;
-webkit-animation-iteration-count:2;
-webkit-animation-direction:alternate
}
@-webkit-keyframes bounce {
from{margin-left:0;}
to{margin-left:250px;}
}
#spin{
-webkit-transition: -webkit-transform 10s ease-in;
}
#spin:hover{
-webkit-transform: rotate(36000deg);
}
.accordion a{
display: block;
padding:5px 10px;
background-color:#ccc;
color:#000;
/*可以去掉链接的下划线等修饰效果*/
text-decoration:none;
}
.accordion a:hover{
background-color:#999;
}
.accordion div{
background-color:#cda;
color:#222;
}
.accordion div p{
padding:20px
}
#accordion div{
/*先隐藏起来*/
height:0;
overflow:hidden;
-webkit-transition:height 600ms ease;
}
#accordion div:target{
height:110px;
}


animation

animation 属性是如下这些属性的简写
animation-name: none
animation-duration: 0s
animation-timing-function: ease
animation-delay: 0s
animation-iteration-count: 1
animation-direction: normal
animation-fill-mode: none

用法

animation:
animation-name
time(duration)
timing-function
time(delay)
animation-iteration-count( 结束之前的循环次数)
single-animation-direction
/*{
animation-direction: normal (每次从正方向开始)
animation-direction: reverse (每次从反方向开始)
animation-direction: alternate (正反往复)
}*/
single-animation-fill-mode

实例

复制代码
代码如下:

<div class="view_port">
<div class="polling_message">
Listener for dispatches
</div>
<div class="cylon_eye">
</div>
</div>
.polling_message {
color: white;
float: left;
margin-right:2%;
}
.view_port {
background-color: black;
height: 50px;
width: 100%;
overflow: hidden;
}
.cylon_eye {
color: white;
height: 100%;
width: 80%;
background-color: red;
background-image: linear-gradient(to right, rgba(0, 0, 0, 0.9) 25%, rgba(0, 0, 0, 0.1) 50%, rgba(0, 0, 0, 0.9) 75%);
-webkit-animation: move_eye 4s linear 0s infinite alternate;
-moz-animation: move_eye 4s linear 0s infinite alternate;
-o-animation: move_eye 4s linear 0s infinite alternate;
animation: move_eye 4s linear 0s infinite alternate;
}
@-webkit-keyframes move_eye {
from {
margin-left:-20%;
}
to {
margin-left:100%;
}
}
@-moz-keyframes move_eye {
from {
margin-left:-20%;
}
to {
margin-left:100%;
}
}
@-o-keyframes move_eye {
from {
margin-left:-20%;
}
to {
margin-left:100%;
}
}
@keyframes move_eye {
from {
margin-left:-20%;
}
to {
margin-left:100%;
}
}
HTML / CSS 相关文章推荐
CSS3 圆角效果
Jul 15 HTML / CSS
css3中background新增的4个新的相关属性用法介绍
Sep 26 HTML / CSS
CSS3实现瀑布流布局与无限加载图片相册的实例代码
Dec 22 HTML / CSS
HTML5 File接口在web页面上使用文件下载
Feb 27 HTML / CSS
简单聊聊H5的pushState与replaceState的用法
Apr 03 HTML / CSS
突袭HTML5之Javascript API扩展1—Web Worker异步执行及相关概述
Jan 31 HTML / CSS
HTML5 video播放器全屏(fullScreen)方法实例
Apr 24 HTML / CSS
localStorage、sessionStorage使用总结
Nov 17 HTML / CSS
Canvas 像素处理之改变透明度的实现代码
Jan 08 HTML / CSS
html5 canvas实现给图片添加平铺水印
Aug 20 HTML / CSS
记一次高分屏下canvas模糊问题
Feb 17 HTML / CSS
分享几个实用的CSS代码块
Jun 10 HTML / CSS
使用CSS3的背景渐变Text Gradient 创建文字颜色渐变
Aug 19 #HTML / CSS
css3中新增的样式使用示例附效果图
Aug 19 #HTML / CSS
css3 position fixed固定居中问题解决方案
Aug 19 #HTML / CSS
CSS3实现多背景展示效果通过CSS3定位多张背景
Aug 10 #HTML / CSS
CSS3实现圆角、阴影、透明效果并兼容各大浏览器
Aug 08 #HTML / CSS
CSS3制作文字半透明倒影效果的两种实现方式
Aug 08 #HTML / CSS
CSS3 transform的skew属性值图文详解
Jul 21 #HTML / CSS
You might like
php 动态执行带有参数的类方法
2009/04/10 PHP
PHP循环遍历数组的3种方法list()、each()和while总结
2014/11/19 PHP
JavaScript 在线压缩和格式化收藏
2009/01/16 Javascript
javaScript 读取和设置文档元素的样式属性
2009/04/14 Javascript
Function.prototype.bind用法示例
2013/09/16 Javascript
Javascript图片上传前的本地预览实例
2014/06/16 Javascript
JQuery 在线引用及测试引用是否成功
2014/06/24 Javascript
jQuery中fadeOut()方法用法实例
2014/12/24 Javascript
jQuery实现的产品自动360度旋转展示特效源码分享
2015/08/21 Javascript
Bootstrap CSS组件之导航条(navbar)
2016/12/17 Javascript
Angular中的$watch、$watchGroup、$watchCollection
2017/06/25 Javascript
vue2.0移除或更改的一些东西(移除index key)
2017/08/28 Javascript
基于vue+canvas的excel-like组件实例详解
2017/11/28 Javascript
Vue.js添加组件操作示例
2018/06/13 Javascript
jQuery的ztree仿windows文件新建和拖拽功能的实现代码
2018/12/05 jQuery
基于Vue.js与WordPress Rest API构建单页应用详解
2019/09/16 Javascript
浅谈layui分页控件field参数接收对象的问题
2019/09/20 Javascript
vue+canvas实现移动端手写签名
2020/05/21 Javascript
如何利用JavaScript编写更好的条件语句详解
2020/08/10 Javascript
vue项目打包为APP,静态资源正常显示,但API请求不到数据的操作
2020/09/12 Javascript
vue中的计算属性和侦听属性
2020/11/06 Javascript
[03:39]DOTA2英雄梦之声_第05期_幽鬼
2014/06/23 DOTA
Python的math模块中的常用数学函数整理
2016/02/04 Python
Python脚本实时处理log文件的方法
2016/11/21 Python
python+requests+unittest API接口测试实例(详解)
2017/06/10 Python
Python 中Pickle库的使用详解
2018/02/24 Python
关于python中plt.hist参数的使用详解
2019/11/28 Python
tornado+celery的简单使用详解
2019/12/21 Python
html5 css3 动态气泡按钮实例演示
2012/12/02 HTML / CSS
HTML5 canvas 瀑布流文字效果的示例代码
2018/01/31 HTML / CSS
20年同学聚会感言
2014/02/03 职场文书
刘胡兰的英雄事迹材料
2014/02/11 职场文书
一年级学生评语
2014/04/23 职场文书
领导干部查摆“四风”问题自我剖析材料思想汇报
2014/10/05 职场文书
2015年文明创建工作总结
2015/04/30 职场文书
浅谈Python中的正则表达式
2021/06/28 Python