CSS3制作各种平滑过渡的动画效果

通过CSS3的新特性,我们可以不使用javascript或Flash就可以将元素从一个状态过渡到另一个状态。它允许元素在指定的时间里改变它们的值,使各种可以动画的属性执行动画效果。


201504072129.jpg




查看演示  下载插件




通过CSS3的新特性,我们可以不使用javascript或Flash就可以将元素从一个状态过渡到另一个状态。它允许元素在指定的时间里改变它们的值,使各种可以动画的属性执行动画效果。CSS3的动画样式和效果使你可以制作出各种你想要的炫酷效果。


在这篇文章中,我们将介绍各种可以在CSS3中使用的过渡和动画效果。


形状变化动画


shape.jpg




对于CSS3元素形状变化的介绍,我们将使用一个圆角矩形图片变化为一个圆形图片为例子,我们将为它添加一个transition效果all 1s ease。


这个属性的意思是当我们用鼠标滑过图片的时候,它将在1秒钟的时间内将图片转换为圆形的形状。整个变形动画会修个图片的border-radius属性,使它从圆角矩形转换为圆形。


这个demo的HTML代码使用一个<div>来包裹一个图片<img>元素,在<div>上使用class morph来制作变形动画。



  1. <div class="morph img"><img alt="" src="img/image1.jpg" /></div>
复制代码


下面来添加一些基本的CSS样式。图片的宽度和高度都设置为200px,并给它一个10像素的边框,然后设置15像素的圆角。另外,在图片的:hover状态下设置鼠标为小手的形状。


  1. .img {
  2.   width: 200px;
  3.   height: 200px;
  4.   border: 10px solid #fff;
  5.   -webkit-border-radius: 15px;
  6.    border-radius: 15px;
  7.   overflow: hidden;
  8. }
  9.   
  10. .img:hover {
  11.   cursor: pointer;
  12. }         
复制代码


现在要制作图形的变形效果,我们将要为这个动画效果添加CSS3 的transition属性。使用这个属性时要为各个浏览器指定厂商前缀:-webkit, -moz, -ms, -o。它的值设置为all 1s ease。transition属性会将元素从正常状态在1秒钟时间内转换到鼠标滑过时的状态。


我们在往下写代码之前,先来看看transition属性的语法:



  1. transition: [transition-property] [transition-duration] [transition-timing-function];     
复制代码


  • [transition-property]:用于指定你想选择或使用的指定样式或属性,例如:background、width等等。在这个demo中,我们指定该属性为all,表示要使用所有可动画的属性进行动画过渡效果,background和padding都包括在内。
  • [transition-duration]:用于指定动画或过渡效果的速度。在这个demo中,它的值为1s,意思是动画将在1秒钟时间内发生。注意,如果不设置这个duration值,transition不会有任何效果。
  • [transition-timing-function]:这个参数用于指定过渡动画的easing效果。该参数的默认值是ease,它的效果是过渡动画开始时很慢,中间变快,然后后面又变慢下来。该属性有6个可能的取值:

  • ease:(逐渐变慢)默认值,ease函数等同于贝塞尔曲线(0.25, 0.1, 0.25, 1.0)。
  • linear:(匀速),linear 函数等同于贝塞尔曲线(0.0, 0.0, 1.0, 1.0)。
  • ease-in:(加速),ease-in 函数等同于贝塞尔曲线(0.42, 0, 1.0, 1.0)。
  • ease-out:(减速),ease-out 函数等同于贝塞尔曲线(0, 0, 0.58, 1.0)。
  • ease-in-out:(加速然后减速),ease-in-out 函数等同于贝塞尔曲线(0.42, 0, 0.58, 1.0)
  • cubic-bezier:(该值允许你去自定义一个时间曲线), 特定的cubic-bezier曲线。 (x1, y1, x2, y2)四个值特定于曲线上点P1和点P2。所有值需在[0, 1]区域内,否则无效。

接下来,我们要为图片的鼠标滑过状态添加样式。我们给边框设置10px的宽度,border-radius属性设置为50%,使它变成一个圆形。同时还添加一些发光的阴影效果。



  1. .morph {
  2.   -webkit-transition: all 1s ease;
  3.   -moz-transition: all 1s ease;
  4.    -ms-transition: all 1s ease;
  5.   -o-transition: all 1s ease;
  6.    transition: all 1s ease;
  7. }
  8.   
  9. .morph:hover {
  10.   border: 10px solid #fff;
  11.    border-radius: 50%;
  12.   -webkit-box-shadow: 0 0 40px rgba(255,255,255,.6), inset 0 0 40px rgba(255,255,255,1);
  13.   -moz-box-shadow: 0 0 40px rgba(255,255,255,.6), inset 0 0 40px rgba(255,255,255,1);
  14.   box-shadow: 0 0 40px rgba(255,255,255,.6), inset 0 0 40px rgba(255,255,255,1);
  15.   
  16. }
复制代码


元素位移动画


displacement1.jpg 


元素的位移动画我们将使用一个缩放效果来作为例子,当鼠标护国图片的时候,它会放大,超出盒子的部分会被隐藏。HTML结构如下:


  1. <div class="zoom img"><img alt="" src="img/image2.jpg" /></div>                       
复制代码


在这个例子中,同样设置200px的图片宽度和高度。这次的过渡效果时间为2秒,在鼠标滑过图片的时候,图片的高度和宽度都被设置为300像素,从而制作出图片缩放的效果。


  1. .zoom img {
  2.   width: 200px;
  3.   height: 200px;
  4.   -webkit-transition: all 2s ease;
  5.   -moz-transition: all 2s ease;
  6.   -ms-transition: all 2s ease;
  7.   -o-transition: all 2s ease;
  8.    transition: all 2s ease;
  9. }
  10.   
  11. .zoom img:hover {
  12.   width: 300px;
  13.   height: 300px;
  14.   -webkit-box-shadow: 0 0 40px rgba(255,255,255,.6), inset 0 0 40px rgba(255,255,255,1);
  15.   -moz-box-shadow: 0 0 40px rgba(255,255,255,.6), inset 0 0 40px rgba(255,255,255,1);
  16.   box-shadow: 0 0 40px rgba(255,255,255,.6), inset 0 0 40px rgba(255,255,255,1);
  17. }                              
复制代码


元素位置动画


position.jpg 


位置动画的过渡效果我们使用图片的旋转作为例子。我们将在0.3秒的时间里将图片旋转10度。HTML结构如下:


  1. <div class="tilt img"><img alt="" src="img/image3.jpg" /></div>      
复制代码


同样为它添加过渡效果,时间为0.3秒。在鼠标滑过时,使用transform属性来旋转图片。transform属性可以使我们旋转、移动、缩放和倾斜元素。同样也在鼠标滑过图片时添加一些发光的阴影效果。


  1. .tilt {
  2.   -webkit-transition: all 0.3s ease;
  3.      -moz-transition: all 0.3s ease;
  4.        -o-transition: all 0.3s ease;
  5.       -ms-transition: all 0.3s ease;
  6.           transition: all 0.3s ease;
  7. }
  8.   
  9. .tilt:hover {
  10.   -webkit-transform: rotate(10deg);
  11.   -moz-transform: rotate(10deg);
  12.   -ms-transform: rotate(10deg);
  13.   -o-transform: rotate(10deg);
  14.    transform: rotate(10deg);
  15.   -webkit-box-shadow: 0 0 40px rgba(255,255,255,.6), inset 0 0 40px rgba(255,255,255,1);
  16.   -moz-box-shadow: 0 0 40px rgba(255,255,255,.6), inset 0 0 40px rgba(255,255,255,1);
  17.    box-shadow: 0 0 40px rgba(255,255,255,.6), inset 0 0 40px rgba(255,255,255,1);
  18. }        
复制代码


颜色过渡效果


color.jpg 


图片颜色的过渡效果我们将用webkit filter过滤器来演示。filter效果是一个图形化的操作,就像一个元素被绘制在页面上一样。HTML结构如下:


  1. <div class="color img"><img alt="" src="img/image4.jpg" /></div>   
复制代码


这次的过渡效果时间设置为1秒钟。在鼠标滑过图片的时候,我们使用webkit-filter属性,并使用grayscale来修改图片的颜色。这个效果只有在webkit内核的浏览器才能看到效果。最后同样也添加一些发光一样效果。


  1. .color {
  2. -webkit-transition: all 1s ease;
  3. -moz-transition: all 1s ease;
  4. -o-transition: all 1s ease;
  5. -ms-transition: all 1s ease;
  6.   transition: all 1s ease;
  7. }
  8.   
  9. .color:hover {
  10.   -webkit-filter: grayscale(100%);
  11.   -webkit-box-shadow: 0 0 40px rgba(255,255,255,.6), inset 0 0 40px rgba(255,255,255,1);
  12.   -moz-box-shadow: 0 0 40px rgba(255,255,255,.6), inset 0 0 40px rgba(255,255,255,1);
  13.    box-shadow: 0 0 40px rgba(255,255,255,.6), inset 0 0 40px rgba(255,255,255,1);
  14.   
  15. }              
复制代码


Tips:你也可以使用blur、sepia、invert、saturate或修改图片的亮度值等方法来制作各种效果。




  • Blur:-webkit-filter: blur(3px);
  • Sepia:-webkit-filter: sepia(100%);
  • Invert:-webkit-filter: invert(0.2);
  • Saturate:-webkit-filter: saturate(50%);




小结


CSS3 transitions可以使元素动画平滑的过渡,这对于我们制作各种CSS3动画时非常有帮助的。建议你在制作CSS3动画时要灵活的使用transition来生成动画效果。



via:http://www.htmleaf.com/ziliaoku/ ... g/201504071643.html

@font-face{ font-family:"Raphaelicons"; src:url('fonts/raphaelicons-webfont.eot'); src:url('fonts/raphaelicons-webfont.eot?#iefix') format('embedded-opentype'), url('fonts/raphaelicons-webfont.ttf') format('truetype'), url('fonts/raphaelicons-webfont.woff') format('woff'), url('fonts/raphaelicons-webfont.svg') format('svg'); font-weight: normal; font-style: normal; } html,body,.container,.st-container{ height: 100%; } body{ font-family:Arial,Georgia,serif; background-color: #ddd; font-weight: bold; font-size: 15px; color: #333; overflow: hidden; -webkit-font-smoothing:antialiased; } a{ text-decoration: none; color: #555; } .clr{ width: 0; height: 0; overflow: hidden; clear: both; padding:0; margin:0; } .st-container{ width: 100%; height: 100%; position: absolute;; top:0; left:0; font-family: Arial,"Josefin Slab","Myriad Pro",sans-serif; } .st-container > input,.st-container > a{ width: 20%; height: 40px; line-height: 40px; position:fixed; bottom:0; cursor: pointer; } .st-container > input{ opacity:0; z-index: 1000; } .st-container > a{ z-index: 10; font-weight: 700; font-size: 16px; background-color: #e23a6e; color: #fff; text-align: center; text-shadow:1px 1px 1px rgba(151,24,64,0.2); -webkit-transition:all 0.5s; -moz-transition:all 0.5s; transition:all 0.5s; } #st-control-1,#st-control-1 + a{ left:0%; } #st-control-2,#st-control-2 + a{ left:20%; } #st-control-3,#st-control-3 + a{ left:40%; } #st-control-4,#st-control-4 + a{ left:60%; } #st-control-5,#st-control-5 + a{ left:80%; } .st-container input:checked + a,.st-container input:checked:hover + a{ background-color: #821134; } .st-container input:checked + a:after{ content: ''; width: 0; height: 0; overflow: hidden; border:20px solid transparent; border-bottom-color:#821134; position: absolute; bottom:100%; left:50%; /*transform:translateX(-50%);*/ margin-left: -20px; } .st-container input:hover + a{ background-color: #ad244f; } /*内容区域*/ .st-scroll, .st-scroll .st-panel{ height: 100%; width: 100%; position: relative; } .st-scroll{ left:0; top:0; -webkit-transform:translate3d(0,0,0); -moz-transform:translate3d(0,0,0); transform:translate3d(0,0,0); -webkit-backface-visibility:hidden; -webkit-transition:all 0.6s ease-in-out; -moz-transition:all 0.6s ease-in-out; transition:all 0.6s ease-in-out; } .st-panel{ background-color: #fff; overflow: hidden; } .st-color{ background-color: #fa96b5; color: #fff; } #st-control-1:checked ~ .st-scroll{ -webkit-transform:translateY(0%); -moz-transform:translateY(0%); transform:translateY(0%); } #st-control-2:checked ~ .st-scroll{ -webkit-transform:translateY(-100%); -moz-transform:translateY(-100%); transform:translateY(-100%); } #st-control-3:checked ~ .st-scroll{ -webkit-transform:translateY(-200%); -moz-transform:translateY(-200%); transform:translateY(-200%); } #st-control-4:checked ~ .st-scroll{ -webkit-transform:translateY(-300%); -moz-transform:translateY(-300%); transform:translateY(-300%); } #st-control-5:checked ~ .st-scroll{ -webkit-transform:translateY(-400%); -moz-transform:translateY(-400%); transform:translateY(-400%); } .st-desc{ width: 200px; height: 200px; background-color: #fa96b5; position: absolute; left:50%; top:0; margin-left: -100px; -webkit-transform:translateY(-50%) rotate(45deg); -moz-transform:translateY(-50%) rotate(45deg); transform:translateY(-50%) rotate(45deg); } .st-color .st-desc{ background-color: #fff; } [data-icon]:after{ content: attr(data-icon); width: 200px; height: 200px; color: #fff; font-size: 90px; text-align: center; line-height: 200px; position: absolute; left: 50%; top:50%; margin: -100px 0 0 -100px; -webkit-transform:rotate(-45deg) translateY(25%); -moz-transform:rotate(-45deg) translateY(25%); transform:rotate(-45deg) translateY(25%); font-family:"Raphaelicons"; text-shadow:1px 1px 1px rgba(151,24,64,0.2); } .st-color [data-icon]:after{ color: #fa96b5; text-shadow:1px 1px 1px rgba(0,0,0,0.1); } .st-panel h2{ color: #e23a6e; font-size: 54px; line-height: 50px; text-align: center; font-weight: 900; width: 80%; position: absolute; left:10%; top:150px; text-shadow: 1px 1px 1px rgba(151,24,64,0.2); -webkit-backface-visibility:hidden; } .st-color h2{ color: #fff; text-shadow:1px 1px 1px rgba(0,0,0,0.1); } #st-control-1:checked ~ .st-scroll #st-panel-1 h2, #st-control-2:checked ~ .st-scroll #st-panel-2 h2, #st-control-3:checked ~ .st-scroll #st-panel-3 h2, #st-control-4:checked ~ .st-scroll #st-panel-4 h2, #st-control-5:checked ~ .st-scroll #st-panel-5 h2{ -webkit-animation:moveDown 0.6s ease-in-out 0.2s backwards; -moz-animation:moveDown 0.6s ease-in-out 0.2s backwards; -o-animation:moveDown 0.6s ease-in-out 0.2s backwards; -ms-animation:moveDown 0.6s ease-in-out 0.2s backwards; animation:moveDown 0.6s ease-in-out 0.2s backwards; } @-webkit-keyframes moveDown{ 0%{ -webkit-transform:translateY(-40px); opacity:0; } 100%{ -webkit-transform:translateY(0px); opacity:1; } } @-moz-keyframes moveDown{ 0%{ -moz-transform:translateY(-40px); opacity:0; } 100%{ -moz-transform:translateY(0px); opacity:1; } } @-o-keyframes moveDown{ 0%{ -o-transform:translateY(-40px); opacity:0; } 100%{ -o-transform:translateY(0px); opacity:1; } } @-ms-keyframes moveDown{ 0%{ -ms-transform:translateY(-40px); opacity:0; } 100%{ -ms-transform:translateY(0px); opacity:1; } } @keyframes moveDown{ 0%{ transform:translateY(-40px); opacity:0; } 100%{ transform:translateY(0px); opacity:1; } } .st-panel p{ font-size: 25px; color: #8b8b8b; line-height: 1.8; text-align: left; text-indent: 2em; font-weight: 700; width: 70%; position: absolute; left:15%; top:280px; text-shadow: 1px 1px 1px rgba(151,24,64,0.2); -webkit-backface-visibility:hidden; } .st-color p{ color:rgba(255,255,255,0.8); text-shadow:1px 1px 1px rgba(0,0,0,0.1); } #st-control-1:checked ~ .st-scroll #st-panel-1 p, #st-control-2:checked ~ .st-scroll #st-panel-2 p, #st-control-3:checked ~ .st-scroll #st-panel-3 p, #st-control-4:checked ~ .st-scroll #st-panel-4 p, #st-control-5:checked ~ .st-scroll #st-panel-5 p{ -webkit-animation:moveUp 0.6s ease-in-out 0.2s backwards; -moz-animation:moveUp 0.6s ease-in-out 0.2s backwards; -o-animation:moveUp 0.6s ease-in-out 0.2s backwards; -ms-animation:moveUp 0.6s ease-in-out 0.2s backwards; animation:moveUp 0.6s ease-in-out 0.2s backwards; } @-webkit-keyframes moveUp{ 0%{ -webkit-transform:translateY(40px); opacity:0; } 100%{ -webkit-transform:translateY(0px); opacity:1; } } @-moz-keyframes moveUp{ 0%{ -moz-transform:translateY(40px); opacity:0; } 100%{ -moz-transform:translateY(0px); opacity:1; } } @-o-keyframes moveUp{ 0%{ -o-transform:translateY(40px); opacity:0; } 100%{ -o-transform:translateY(0px); opacity:1; } } @-ms-keyframes moveUp{ 0%{ -ms-transform:translateY(40px); opacity:0; } 100%{ -ms-transform:translateY(0px); opacity:1; } } @keyframes moveUp{ 0%{ transform:translateY(40px); opacity:0; } 100%{ transform:translateY(0px); opacity:1; } } @media screen and (max-width:520px){ .st-panel h2{ font-size: 42px; } .st-panel p{ font-size: 18px; width: 90%; left:5%; top:240px; } .st-container > a{ font-size: 13px; } .st-desc{ width: 160px; height:160px; margin-left: -80px; } [data-icon]:after{ font-size: 75px; -webkit-transform:rotate(-45deg) translateY(20%); -moz-transform:rotate(-45deg) translateY(20%); transform:rotate(-45deg) translateY(20%); } } @media screen and (max-width:360px){ .st-panel h2{ font-size: 42px; } .st-panel p{ font-size: 18px; width: 90%; left:5%; top:240px; } .st-container > a{ font-size: 10px; } .st-desc{ width: 120px; height:120px; margin-left: -60px; } [data-icon]:after{ font-size: 60px; -webkit-transform:rotate(-45deg) translateY(15%); -moz-transform:rotate(-45deg) translateY(15%); transform:rotate(-45deg) translateY(15%); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值