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绘制出各种几何图形
Aug 17 HTML / CSS
介绍CSS3使用技巧5个
Apr 02 HTML / CSS
收集的22款给力的HTML5和CSS3帮助工具
Sep 14 HTML / CSS
详解CSS3中@media的实际使用
Aug 04 HTML / CSS
CSS3制作缩略图的详细过程
Jul 08 HTML / CSS
使用css3做0.5px的细线的示例代码
Jan 18 HTML / CSS
HTML5之SVG 2D入门9—蒙板及mask元素介绍与应用
Jan 30 HTML / CSS
html5 worker 实例(一) 为什么测试不到效果
Jun 24 HTML / CSS
关于HTML5语义标签的实践(blog页面)
Jul 12 HTML / CSS
HTML5和CSS3实例教程总结(推荐)
Jul 18 HTML / CSS
Html+Css+Jquery实现左侧滑动拉伸导航菜单栏的示例代码
Mar 17 HTML / CSS
Html5页面上如何禁止手机虚拟键盘弹出
Mar 19 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的cURL库功能简介 抓取网页、POST数据及其他
2011/04/07 PHP
PHP实现C#山寨ArrayList的方法
2015/07/16 PHP
joomla组件开发入门教程
2016/05/04 PHP
Zend Framework教程之Zend_Helpers动作助手ViewRenderer用法详解
2016/07/20 PHP
PHP实现的mongoDB数据库操作类完整实例
2018/04/10 PHP
如何用javascript去掉字符串里的所有空格
2007/02/08 Javascript
Prototype 学习 工具函数学习($w,$F方法)
2009/07/12 Javascript
javascript克隆对象深度介绍
2012/11/20 Javascript
基于js disabled=&quot;false&quot;不起作用的解决办法
2013/06/26 Javascript
jQuery 事件的命名空间简单了解
2013/11/22 Javascript
jquery实现鼠标滑过小图时显示大图的方法
2015/01/14 Javascript
JavaScript和JQuery的鼠标mouse事件冒泡处理
2015/06/19 Javascript
卸载安装Node.js与npm过程详解
2016/08/15 Javascript
javascript实现滑动解锁功能
2017/03/22 Javascript
bootstrap select插件封装成Vue2.0组件
2017/04/17 Javascript
layui框架中layer父子页面交互的方法分析
2017/11/15 Javascript
单页面vue引入百度统计的使用方法示例详解
2018/10/13 Javascript
其实你可以少写点if else与switch(推荐)
2019/01/10 Javascript
js刷新页面location.reload()用法详解
2019/12/09 Javascript
vue实现购物车选择功能
2020/01/10 Javascript
Vue与React的区别和优势对比
2020/12/18 Vue.js
浅谈Python中的zip()与*zip()函数详解
2018/02/24 Python
Django csrf 验证问题的实现
2018/10/09 Python
Python3之手动创建迭代器的实例代码
2019/05/22 Python
python3字符串操作总结
2019/07/24 Python
DataFrame.to_excel多次写入不同Sheet的实例
2019/12/02 Python
西铁城美国官方网站:Citizen Watch美国
2019/11/08 全球购物
信息技术专业大学生个人的自我评价
2013/10/05 职场文书
幼儿教师国培感言
2014/02/19 职场文书
会计岗位职责模板
2014/03/12 职场文书
珍惜资源保护环境的建议书
2014/05/14 职场文书
做一个有道德的人活动方案
2014/08/25 职场文书
学生逃课检讨书1000字
2014/10/20 职场文书
2015年领导干部廉洁自律工作总结
2015/05/26 职场文书
CSS3实现的水平标题菜单
2021/04/14 HTML / CSS
在虚拟机中安装windows server 2008的图文教程
2022/06/28 Servers