弄清 CSS3 的 transition 和 animation

transition

transition 属性是

transition-property,

transition-duration,

transition-timing-function,

transition-delay

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

所以 transition 的初始值为:

  1. transition-delay: 0s;
  2. transition-duration: 0s;
  3. transition-property: all;
  4. transition-timing-function: ease;

用法

  1. div {
  2. transition: <property> <duration> <timing-function> <delay>;
  3. }

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

  1. el.addEventListener("transitionend",updateTransition,true);
  2. //in webkit
  3. el.addEventListener("webkitTransitionEnd",updateTransition,true);

实例

HTML

  1. <!-- DEMO 1: Fade Block -->
  2. <div id="fade">
  3. move here !
  4. </div>
  5. <div id="nudge">
  6. mouse on me
  7. </div>
  8. <div id="bounce">Place mouse on me i will bounce!</div>
  9. <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>
  10. <div id="accordion" class="accordion">
  11. <a href="#first">This is first tab</a>
  12. <div id="first"><p>Lorem ipsum </p> </div>
  13. <a href="#second">This is second tab</a>
  14. <div id="second"><p>Lorem ipsum </p> </div>
  15. <a href="#third">This is third tab</a>
  16. <div id="third"><p>Lorem ipsum </p> </div>
  17. </div>

CSS

  1. /*
  2. DEMO 1: Fade Block
  3. */
  4. div {
  5. margin-bottom: 50px;
  6. }
  7. #fade {
  8. /*opacity:1;
  9. -webkit-transition: opacity 10s liner 10s;*/
  10. position: relative;
  11. transition-property: font-size;
  12. transition-duration: 0.5s;
  13. transition-delay: 0;
  14. font-size: 14px;
  15. }
  16. #fade:hover {
  17. font-size: 36px;
  18. }
  19. /* DEMO2 */
  20. #nudge{
  21. -webkit-transition-property: color,
  22. background-color,padding-left;
  23. -webkit-transition-duration: 500ms,500ms, 500ms;
  24. }
  25. #nudge:hover{
  26. background-color: #efefef;
  27. color: #333;
  28. padding-left: 50px;
  29. }
  30. #bounce:hover {
  31. -webkit-animation-name:bounce;
  32. -webkit-animation-duration:1s;
  33. -webkit-animation-iteration-count:2;
  34. -webkit-animation-direction:alternate
  35. }
  36. @-webkit-keyframes bounce {
  37. from{margin-left:0;}
  38. to{margin-left:250px;}
  39. }
  40. #spin{
  41. -webkit-transition: -webkit-transform 10s ease-in;
  42. }
  43. #spin:hover{
  44. -webkit-transform: rotate(36000deg);
  45. }
  46. .accordion a{
  47. display: block;
  48. padding:5px 10px;
  49. background-color:#ccc;
  50. color:#000;
  51. /*可以去掉链接的下划线等修饰效果*/
  52. text-decoration:none;
  53. }
  54. .accordion a:hover{
  55. background-color:#999;
  56. }
  57. .accordion div{
  58. background-color:#cda;
  59. color:#222;
  60. }
  61. .accordion div p{
  62. padding:20px
  63. }
  64. #accordion div{
  65. /*先隐藏起来*/
  66. height:0;
  67. overflow:hidden;
  68. -webkit-transition:height 600ms ease;
  69. }
  70. #accordion div:target{
  71. height:110px;
  72. }

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

用法

  1. animation:
  2. animation-name
  3. time(duration)
  4. timing-function
  5. time(delay)
  6. animation-iteration-count( 结束之前的循环次数)
  7. single-animation-direction
  8. /*{
  9. animation-direction: normal (每次从正方向开始)
  10. animation-direction: reverse (每次从反方向开始)
  11. animation-direction: alternate (正反往复)
  12. }*/
  13. single-animation-fill-mode

实例

  1. <div class="view_port">
  2. <div class="polling_message">
  3. Listener for dispatches
  4. </div>
  5. <div class="cylon_eye">
  6. </div>
  7. </div>
  1. .polling_message {
  2. color: white;
  3. float: left;
  4. margin-right:2%;
  5. }
  6. .view_port {
  7. background-color: black;
  8. height: 50px;
  9. width: 100%;
  10. overflow: hidden;
  11. }
  12. .cylon_eye {
  13. color: white;
  14. height: 100%;
  15. width: 80%;
  16. background-color: red;
  17. 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%);
  18. -webkit-animation: move_eye 4s linear 0s infinite alternate;
  19. -moz-animation: move_eye 4s linear 0s infinite alternate;
  20. -o-animation: move_eye 4s linear 0s infinite alternate;
  21. animation: move_eye 4s linear 0s infinite alternate;
  22. }
  23. @-webkit-keyframes move_eye {
  24. from {
  25. margin-left:-20%;
  26. }
  27. to {
  28. margin-left:100%;
  29. }
  30. }
  31. @-moz-keyframes move_eye {
  32. from {
  33. margin-left:-20%;
  34. }
  35. to {
  36. margin-left:100%;
  37. }
  38. }
  39. @-o-keyframes move_eye {
  40. from {
  41. margin-left:-20%;
  42. }
  43. to {
  44. margin-left:100%;
  45. }
  46. }
  47. @keyframes move_eye {
  48. from {
  49. margin-left:-20%;
  50. }
  51. to {
  52. margin-left:100%;
  53. }
  54. }

弄清 CSS3 的 transition 和 animation的更多相关文章

  1. CSS3动画 transition和animation的用法和区别

    transition和animation都是CSS3新增的特性,使用时需要加内核 浏览器 内核名称 W3C   IE  -ms-  Chrome/Safari -webkit-   Firefoc - ...

  2. 2018年1月17日总结 css3里transition 和animation 区别

    transition 和animation两个CSS3属性经常被用到实际项目中,想把它整理出来. 1.先介绍transition >>>>>  a. 在做项目中经常会遇见 ...

  3. css3,transition,animation两种动画实现区别

    我们为页面设置动画时,往往会用到transition还有animation以及transfrom属性或者用到js. 其实通常情况下,对于使用js我们更加倾向于使用css来设置动画. transfrom ...

  4. css3中transition和animation的回调处理

    弱鸡最近在准备面试,网上找了一些题,发现一些基础题也完全答不好(┬_┬)看来还是要再接再励啊w(゚Д゚)w 言归正传,今天的主题是CSS3中的动画回调处理,这里动画执行完毕后触发的事件是transit ...

  5. css3 transition 和 animation实现走马灯

    这段时间在做一个App,H5的开发.页面上有公告 以走马灯的形式显示出来. 在开始直接用的marquee标签,后来发现在ios客户端,走马灯移动不够平滑,有抖动现象. 对于有强迫症的我而言是无法忍受的 ...

  6. css3动画入门transition、animation

    css3动画 transition.animation CSS3 transition demo <!DOCTYPE html> <html> <head> < ...

  7. css3实践之图片轮播(Transform,Transition和Animation)

    楼主喜欢追求视觉上的享受,虽常以牺牲性能无法兼容为代价却也乐此不疲.本文就通过一个个的demo演示来简单了解下css3下的Transform,Transition和Animation. 本文需要实现效 ...

  8. CSS3中动画属性transform、transition和animation

    Transform:变形 在网页设计中,CSS被习惯性的理解为擅长表现静态样式,动态的元素必须借助于javascript才可以实现,而CSS3的出现改变了这一思维方式.CSS3除了增加革命性的创新功能 ...

  9. CSS3中动画属性transform、transition 和 animation

    CSS3中和动画有关的属性有三个 transform.transition 和 animation.下面来一一说明:        transform   从字面来看transform的释义为改变,使 ...

随机推荐

  1. Doctype的作用

    <!DOCTYPE> 声明不是 HTML 标签:它是指示 web 浏览器关于页面使用哪个 HTML 版本进行编写的指令,也就是定义文档类型. 所有的浏览器都需要两种模式:怪异模式和严格模式 ...

  2. .NET积累

    2016-10-27 给视图中的select赋值: 控制器: public ActionResult Add() { List<SelectListItem> ClassName = ne ...

  3. Mongodb系统管理员权限设置

    管理员账号无法执行show dbs .show collections Js代码   { "_id" : ObjectId("52a82bb26cea234c4deb06 ...

  4. WPF中弹出菜单

    在WPF里弹出菜单是用Popup,你那个右键的是上下文菜单(也就是快捷菜单). <Grid> <Button x:Name="BtnPop" Width=&quo ...

  5. 163邮件出错:不允许使用邮箱名称。 服务器响应为: authentication is required,smtp7,C8CowEDpS0+Uke9VvSmXBg--.546S2 1441763733

    原因:用163邮箱发邮件,需开启smtp服务,开启服务时,要求使用客户端授权码. 在.net中,使用smtp发邮件,在验证中使用的密码,是上面所讲的客户端授权码,而不是注册和web登录时用的邮箱密码. ...

  6. VC++ 设置软件开机自启动的方法

    0  概述 软件开机自启动是比较常用的做法,设置方法也有好几种. 1  使用者模式 在"开始菜单"的所有程序中有个"启动"文件夹,可以将需要设置为开机启动的应用 ...

  7. kettle系列-kettle管理平台部署说明

    本介绍我的开源项目[kettle-manager]kettle管理平台如何获取并部署使用,该项目介绍请参看另一篇博文:http://www.cnblogs.com/majinju/p/5739820. ...

  8. 同一行多个div宽度自适应布局

    主要运用到的是:布局神器display:table-cell 元素两端对齐 第一个案例是让两个元素分别向左和向右对齐,如果是过去,我一定会用float来实现,但其实用table可以这么做: 自动平均划 ...

  9. 安天移动安全应对“DressCode”威胁,发布企业移动威胁检查工具

    近日,一种名为"DressCode"的恶意代码引起了国内安全行业的关注,该恶意代码以企业员工的移动设备作为跳板对企业内网进行攻击,对企业安全造成严重威胁.安天移动安全公司威胁情报团 ...

  10. vue简单使用

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...