CSS3中Transition动画属性用法详解

夜景如诗,豪放浪漫;夜景似文,美妙动人,夜景如画,在笔墨的淡妆浓抹里,星星月亮与灯光呈现出一幅灿烂的画卷。

W3C标准中对css3的transition这是样描述的:“css的transition允许css的属性值在一定的时间区间内平滑地过渡。这种效果可以在鼠标单击、获得焦点、被点击或对元素任何改变中触发,并圆滑地以动画效果改变CSS的属性值。”

transition属性的值包括以下四个:

•transition-property: 指定对HTML元素的哪个css属性进行过渡渐变处理,这个属性可以是color、width、height等各种标准的css属性。
•transition-duration:指定属性过渡的持续时间
•transition-timing-function:指定渐变的速度:
1、ease:(逐渐变慢)默认值,ease函数等同于贝塞尔曲线(0.25, 0.1, 0.25, 1.0);
2、linear:(匀速),linear 函数等同于贝塞尔曲线(0.0, 0.0, 1.0, 1.0);
3、ease-in:(加速),ease-in 函数等同于贝塞尔曲线(0.42, 0, 1.0, 1.0);
4、ease-out:(减速),ease-out 函数等同于贝塞尔曲线(0, 0, 0.58, 1.0);
5、ease-in-out:(加速然后减速),ease-in-out 函数等同于贝塞尔曲线(0.42, 0, 0.58, 1.0);
6、cubic-bezier:(该值允许你去自定义一个时间曲线), 特定的cubic-bezier曲线。 (x1, y1, x2, y2)四个值特定于曲线上点P1和点P2。所有值需在[0, 1]区域内,否则无效。
•transition-delay:指定延迟时间,也就是经过多长时间才开始执行过渡过程。

浏览器兼容性

Internet Explorer 9 以及更早的版本,不支持 transition 属性。

Internet Explorer 10, Firefox, Opera 和 Chrome支持transition 属性。Chrome 25 以及更早的版本以及Safari 需要前缀 -webkit-。

下面还是以实例来说明transition的用法

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPEhtml>
  2. <htmllang="en">
  3. <head>
  4. <metacharset="UTF-8">
  5. <title>transition演示1</title>
  6. <styletype="text/css">
  7. .animated_div{
  8. margin:100pxauto;
  9. width:100px;
  10. height:60px;
  11. background:#92B901;
  12. /*简写属性*/
  13. -webkit-transition:-webkit-transform1s,opacity1s,background1s,width1s,height1s,font-size1s;/*Safari*/
  14. /*每个属性分开写*/
  15. transition-property:width,height,transform,background,opacity;
  16. transition-duration:1s,1s,1s,1s,1s,1s;
  17. -webkit-border-radius:5px;
  18. border-radius:5px;
  19. opacity:0.4;
  20. }
  21. .animated_div:hover{
  22. -moz-transform:rotate(360deg);
  23. -webkit-transform:rotate(360deg);
  24. -o-transform:rotate(360deg);
  25. transform:rotate(360deg);
  26. opacity:1;
  27. background:#1ec7e6;
  28. width:200px;
  29. height:120px;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <divclass="animated_div"></div>
  35. </body>
  36. </html>

上述代码当鼠标移到div上时,CSS属性:width,height,transform,background,opacity都发生渐变过渡效果。最终css样式变成.animated_div里定义的样式,过渡过程大致如下:

再给一个网上的嫦娥奔月的示例,要求:当鼠标移到图片上时,嫦娥出现,移开时嫦娥消失

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPEhtml>
  2. <html>
  3. <head>
  4. <metacharset="utf-8">
  5. <title>transition演示2</title>
  6. <styletype="text/css">
  7. body{
  8. color:#fff;
  9. background:#000;
  10. }
  11. .change{
  12. display:block;
  13. width:400px;
  14. height:400px;
  15. background:url(http://p3.qhimg.com/t0134c65e59012a1257.png)no-repeatcenter;
  16. background-size:cover;
  17. border:1emsolidrgba(255,255,255,.8);
  18. margin:50pxauto;
  19. }
  20. .changeimg{
  21. display:block;
  22. width:300px;
  23. height:284px;
  24. opacity:0;
  25. -webkit-transform:translate(-100px,-100px);
  26. transform:translate(-100px,-100px);
  27. -webkit-transition:opacity1sease-in-out0.5s,-webkit-transform1sease-in-out;
  28. transition:opacity1sease-in-out0.5s,transform1sease-in-out;
  29. }
  30. .change:hoverimg{
  31. -webkit-transform:translate(0px,0px);
  32. transform:translate(0px,0px);
  33. opacity:1;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <arel="nofollow noopener noreferrer" href="http://image.haosou.com/i?q=%E5%AB%A6%E5%A8%A5png&src=tab_www"class="change"target="_blank">
  39. <imgsrc="http://p4.qhimg.com/t0160e6a92121691e22.png"alt=""/>
  40. </a>
  41. </body>
  42. </html>

为了使嫦娥有飘入飘出的效果,设置了transform属性,配合opacity属性,加入过渡效果就能达到效果:

本文CSS3中Transition动画属性用法详解到此结束。吸取教训是健康的做法,这是个人成长过程中的必要环节。悔恨则是一种不健康的心理。这是白白浪费自我目前的精力。小编再次感谢大家对我们的支持!

标签: Transition