css动画属性使用及实例代码(transition/transform/animation)

所属分类: 网页制作 / CSS 阅读数: 205
收藏 0 赞 0 分享

在开发中,一个好的用户操作界面,总会夹杂着一些动画。css用对少的代码,来给用户最佳的体验感,下面我总结了一些css动画属性的使用方法及用例代码供大家参考,在不对的地方,希望大佬直接拍砖评论。

1 transition(过渡)

  使用语法:

transition: property duration timing-function delay;

  参数:

  (1) property(设置过渡效果的css属性名称):none | all | property。none表示没有属性获得过渡效果;all表示所有属性都将获得过渡效果;property表示css属性列表,多个属性用逗号( , )隔开。

  (2) duration(设置完成过渡效果的时间):秒或毫秒(s/ms)。

  (3) timing-function(设置效果速度的速度曲线):linear,规定以相同速度开始到结束,等价于cubic-bezier(0,0,1,1);ease,慢速开始,然后慢速结束,等价于cubic-bezier(0.25,0.1,0.25,1);ease-in,以慢速开始,等价于cubic-bezier(0.42,0,1,1);ease-out,以慢速结束,等价于cubic-bezier(0,0,0.58,1);ease-in-out,以慢速开始和结束,等价于cubic-bezier(0.42,0,0.58,1);cubic-bezier(n,n,n,n),在该函数定义自己的值,数值为0-1之间。

  (4) delay(过渡效果何时开始):值多少秒后执行过渡效果,如 2s ,表示2s后执行。

2 transform

  transform属性应用于2D 或 3D转换。该属性允许我们能够对元素进行旋转、缩放、倾斜、移动这四类操作。

  使用语法:

transform: none|transform-functions;

  参数:

  (1) none:定义不进行任何转换,一般用于注册掉该转换。

  (2) transform-functions:定义要进行转换的类型函数。主要有:

    旋转(rotate):主要分为2D旋转和3D旋转。rotate(angle),2D 旋转,参数为角度,如45deg;rotate(x,y,z,angle),3D旋转,围绕原地到(x,y,z)的直线进行3D旋转;rotateX(angle),沿着X轴进行3D旋转;rotateY(angle);rotateZ(angle);

    缩放(scale):一般用于元素的大小收缩设定。主要类型同上,有scale(x, y)、scale3d(x, y, z)、scaleX(x)、scaleY(y)、scaleZ(z),其中x、y、z为收缩比例。

    倾斜(skew):主要用于对元素的样式倾斜。skew(x-angle, y-angle),沿着x和y轴的2D倾斜转换;skewX(angle),沿着x轴的2D倾斜转换;skew(angle),沿着y轴的2D倾斜转换。

    移动(translate):主要用于将元素移动。translate(x, y),定义向x和y轴移动的像素点;translate(x, y, z),定义像x、y、z轴移动的像素点;translateX(x);translateY(y);translateZ(z)。

3 animation

  该属性主要用于设置动画属性。

  使用语法:

animation: name duration timing-function delay iteration-count direction;

  参数:

    (1) name:需要绑定到选择器的keyframe名称。

    (2) duration:完成该动画需要花费的时间,秒或毫秒。

    (3) timing-function:动画的运动速度曲线。linear,规定以相同速度开始到结束,等价于cubic-bezier(0,0,1,1);ease,慢速开始,然后慢速结束,等价于cubic-bezier(0.25,0.1,0.25,1);ease-in,以慢速开始,等价于cubic-bezier(0.42,0,1,1);ease-out,以慢速结束,等价于cubic-bezier(0,0,0.58,1);ease-in-out,以慢速开始和结束,等价于cubic-bezier(0.42,0,0.58,1);cubic-bezier(n,n,n,n),在该函数定义自己的值,数值为0-1之间。

    (4) delay:设置动画在开始之前的延迟。

    (5) iteration-count:设置动画执行的次数。

    (6) direction:是否轮询反向播放动画。normal,默认值,动画应该正常播放;alternate,动画应该轮流反向播放。

下面展示了这些元素的测试代码:

<!DOCTYPE html>
<html>
<head>
    <title>transition/transform</title>
</head>
<style type="text/css">
    #div1 {
        float: left;
        height: 100px;
        width: 100px;
        background-color: red;
    }
    #div2 {
        float: left;
        height: 100px;
        width: 100px;
        background-color: green;
    }
    #div3 {
        float: left;
        height: 100px;
        width: 100px;
        background-color: blue;
    }
    #div4 {
        float: left;
        height: 100px;
        width: 100px;
        background-color: #234F21;
    }
    #div5 {
        float: left;
        height: 100px;
        width: 100px;
        background-color: #af123c;
    }
    #div6 {
        float: left;
        height: 100px;
        width: 100px;
        background-color: #affa3c;
    }
    /* transition 实现多个属性 */
    #div1:active {
        width:200px;
        height: 200px;
        transition: width 2s ease,height 2s ease;
        -moz-transition: width 2s ease,height 2s ease; /* Firefox 4 */
        -webkit-transition: width 2s ease,height 2s ease; /* Safari 和 Chrome */
        -o-transition: width 2s ease,height 2s ease; /* Opera */
    }
    /* transform 旋转 rotate */
    #div2:hover {
        transform:rotate(35deg);
        -ms-transform:rotate(35deg);     /* IE 9 */
        -moz-transform:rotate(35deg);     /* Firefox */
        -webkit-transform:rotate(35deg); /* Safari 和 Chrome */
        -o-transform:rotate(35deg);     /* Opera */
    }
    /* transform 缩放 scale */
    #div3:hover {
        transform:scale(0.8, 1.5);
        -ms-transform:scale(0.8, 1.5);     /* IE 9 */
        -moz-transform:scale(0.8, 1.5);     /* Firefox */
        -webkit-transform:scale(0.8, 1.5); /* Safari 和 Chrome */
        -o-transform:scale(0.8, 1.5);     /* Opera */
    }
    /* transform 倾斜 skew */
    #div4:hover {
        transform:skew(35deg);
        -ms-transform:skew(35deg);     /* IE 9 */
        -moz-transform:skew(35deg);     /* Firefox */
        -webkit-transform:skew(35deg); /* Safari 和 Chrome */
        -o-transform:skew(35deg);     /* Opera */
    }
    /* transform 移动 translate */
    #div5:hover {
        transform:translate(45px, 45px);
        -ms-transform:translate(45px, 45px);     /* IE 9 */
        -moz-transform:translate(45px, 45px);     /* Firefox */
        -webkit-transform:translate(45px, 45px); /* Safari 和 Chrome */
        -o-transform:translate(45px, 45px);     /* Opera */
    }
    /* transform 多个效果 */
    #div6:hover {
        transform:rotate(35deg) scale(0.8, 1.5) skew(35deg) translate(45px, 45px);
        -ms-transform:rotate(35deg) scale(0.8, 1.5) skew(35deg) translate(45px, 45px);     /* IE 9 */
        -moz-transform:rotate(35deg) scale(0.8,rotate(35deg) scale(0.8, 1.5) skew(35deg) translate(45px, 45px)translate(45px, 45px); /* Safari 和 Chrome */
        -o-transform:rotate(35deg) scale(0.8, 1.5) skew(35deg) translate(45px, 45px);     /* Opera */
    }
</style>
<body>
    <div id="div1">transition</div>
    <div id="div2">transform rotate</div>
    <div id="div3">transform scale</div>
    <div id="div4">transform skew</div>
    <div id="div5">transform translate</div>
    <div id="div6">transform</div>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
    <title>transition/transform</title>
</head>
<style type="text/css">
    /* animation */
    .div7 {
        width:100px;
        height:100px;
        background:red;
        position:relative;
        animation:myfirst 5s infinite;
        animation-direction:alternate;
        /* Safari and Chrome */
        -webkit-animation:myfirst 5s infinite;
        -webkit-animation-direction:alternate;
    }
    @keyframes myfirst{
        0%   {background:red; left:0px; top:0px;}
        25%  {background:yellow; left:200px; top:0px;}
        50%  {background:blue; left:200px; top:200px;}
        75%  {background:green; left:0px; top:200px;}
        100% {background:red; left:0px; top:0px;}
    }
    @-webkit-keyframes myfirst {/* Safari and Chrome */
        0%   {background:red; left:0px; top:0px;}
        25%  {background:yellow; left:200px; top:0px;}
        50%  {background:blue; left:200px; top:200px;}
        75%  {background:green; left:0px; top:200px;}
        100% {background:red; left:0px; top:0px;}
    }
  @-moz-keyframes myfirst {/* Firefox */
     0%   {background:red; left:0px; top:0px;}
        25%  {background:yellow; left:200px; top:0px;}
        50%  {background:blue; left:200px; top:200px;}
        75%  {background:green; left:0px; top:200px;}
        100% {background:red; left:0px; top:0px;}
  }
  @-o-keyframes myfirst {/* Opera */
     0%   {background:red; left:0px; top:0px;}
        25%  {background:yellow; left:200px; top:0px;}
        50%  {background:blue; left:200px; top:200px;}
        75%  {background:green; left:0px; top:200px;}
        100% {background:red; left:0px; top:0px;}
  }
</style> <body> <div class="div7">animation</div> </body> </html>

以上所述是小编给大家介绍的css动画属性使用及实例代码(transition/transform/animation) ,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

更多精彩内容其他人还在看

js 解决隐藏与显示div的相关问题

我的导航中就有一栏产品中心下面用隐藏个div然后鼠标放上去就显示出来,但是导航那一块div一直出不来,什么都没有,很是郁闷不知道是什么原因
收藏 0 赞 0 分享

关于clearBoth在GOOGLE Chrome中的问题解决方法

下面这段CSS在IE中好好的,但在GOOGLE Chrome中总是不行,我调测了无数次。问题就出在 clearBoth 这个样式上,此问题如何解决,写下来详细介绍
收藏 0 赞 0 分享

用css margin去掉横排图片之间的间距

HTM,CSS,怎样去掉横排图片之间的间距,是我们的一大头疼问题,于是本人搜集整理一下,晒出来和大家分享,希望可以帮助你们
收藏 0 赞 0 分享

关于li:hover的怎么清除浮动问题实现代码

当鼠标移动上去时,周围显示一个方框,但是后面的会向后移动,如何才能使得当鼠标移上去时后面的li不浮动
收藏 0 赞 0 分享

ie6不支持两个连续并列class类名怎么解决

在网页布局中会使用到两个连续的class,但唯独ie6不支持,很郁闷,于是搜索整理下,晒出来和大家分享
收藏 0 赞 0 分享

如何在class内写xsl标记注意事项

想要在class中写一个xsl标记,想要知道应该如何来写出正确的代码语句,请详看本文
收藏 0 赞 0 分享

ie10 css hack 条件注释等兼容方式整理

ie10已经上线一段时间了,相信已经有一部分前端潮人体验过了,截至到现在,在ie6到ie9的浏览器各种各样的古怪行为,开发人员不得不使用条件注释,有条件的类,和其他特定于IE的css hack来解决
收藏 0 赞 0 分享

IE6双倍边距 IE6浏览器会出现双倍边距解决方法

所谓的IE6双倍边距就是指当元素有float属性,又有margin属性时,在IE6下面显示的margin的值是设置值的两倍,这个问题从有css技术时就已经诞生,本文将介绍详细解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

css 网页背景图片 怎样用CSS实现大背景网页效果

在网页设计制作中经常会遇到这样的问题:用图片做背景时,由于显示器分辨率太大或者图片尺寸太小,在页面的两边或者下部了没有背景图片,使页面变的很僵硬,于是搜集整理一些,晒出来和大家分享
收藏 0 赞 0 分享

CSS字体中英文名称对照表 CSS常用中文字体英文名称对照表

在CSS文件中,我们常看到有些字体名称变成了乱码,这是由于编写者将中文字体的名字直接写成了中文,为了避免这种状况出现,在CSS文件中使用中文字体时,最好使用中文字体的英文名称,需要的朋友可以注意下
收藏 0 赞 0 分享
查看更多