css动画(transition/transform/animation)

在开发中,一个好的用户操作界面,总会夹杂着一些动画。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>

原文地址:https://www.cnblogs.com/www-123456/p/10837619.html

时间: 2024-08-01 03:11:56

css动画(transition/transform/animation)的相关文章

关于CSS的Transition,Transform,Animation

Transform:DOM变形 Transition:某个DOM或者多个DOM变化,只有两个关键帧. Animation:生成动画,可以多个关键帧 Transition 需要触发的时事件. Animation不需要 关于CSS的Transition,Transform,Animation,布布扣,bubuko.com

css动画——transition和animation

http://www.ruanyifeng.com/blog/2014/02/css_transition_and_animation.html 第一部分:CSS Transition 在CSS 3引入Transition(过渡)这个概念之前,CSS是没有时间轴的.也就是说,所有的状态变化,都是即时完成. delay的真正意义在于,它指定了动画发生的顺序,使得多个不同的transition可以连在一起,形成复杂效果. transition的使用注意 (1)目前,各大浏览器(包括IE 10)都已经

css 动画 transition和animation

本文参考:http://www.ruanyifeng.com/blog/2014/02/css_transition_and_animation.html 1. transition基本用法: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="

那些好玩的CSS - transition transform animation - 北大青鸟教员授课技术交流会

那些好玩的CSS - transition transform animation 北大青鸟教员授课技术交流会 前言: 无意间翻到了之前做的一个PPT,就想着发出来给大家分享一下. 因为公司招了不少新员工,所以经理借着那次的授课交流会,让大家彼此学习一下授课技巧 上课方式 课堂互动等等. 我做的这个是用的极客学院的PPT风格,简单地介绍了一下CSS3里面的几个动画相关的东西,transition过渡.transform变换.animation自定义动画等等. PPT内容如下: (PS:如果需要P

css——动画(transform, transition, animation)

transform 静态属性,一旦写进style里面,会立即显示作用,无任何变化过程.(类似于left, right, top, bottom这类属性) 主要用来做元素的变形 改变元素样式的属性主要有以下五个 translate3d(x,y,z) 用来控制元素在页面的三轴上的位置 rotate(10deg) 是用来控制元素旋转角度(度deg) skewx,y 制作斜度,2d里面创建3d透视图的必备属性 scale3d(2, 1.5, -6) 用来放大缩小效果,属性是比值 matrix3d css

关于css3 transition transform animation属性

1,transition属性 个人非常习惯用transition过度属性,简单易用.大家称他为animation简化版本. 例如: .contain{ width: 392px; position: relative; bottom: -20px; opacity: 0;} .contain.on{ bottom: 0; opacity: 1; transition:all 500ms ease-out 2s; -webkit-animation:all 500ms ease-out 2s; -

CSS动画效果之animation

Y(^o^)Y css动画大乱弹之animation. 概述 什么是animation呢?在回答这个问题之前,先要说明什么叫做@keyframe(关键帧).@keyframe算是一个动画模板.在其中,可以使用百分比,如从0%到100%的任意值,分别在每个百分比中,加上不同的属性,从而让元素达到一种在不断变化的动画效果.这和我们制作flash动画一样,我们只需设计几个关键帧,系统就能生成动画啦! 一个@keyframe例子: 1 /*定义关键帧动画*/ 2 @keyframes myframe {

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

transition和animation都是CSS3新增的特性,使用时需要加内核 浏览器 内核名称 W3C   IE  -ms-  Chrome/Safari -webkit-   Firefoc -moz-   opera -o-  区别: transition是过度属性,需要用户自行触发,触发时间比如:点击,鼠标悬浮等 animation是动画属性,其不需要用户触发,网页加载完成后自动执行 使用: transation{过度属性 过度时间 动画类型 延迟时间} -o-transation{过

css3 三大王 transition , transform , animation

三大王  transition : 过渡     , transform : 变形  ,   animation : 动画 1.transform  变形 任何的变形都可以被过渡   , 一个transform写多个用空格隔开 ,分开写可能会被覆盖, 想要实现3d效果要给父元素添加景深 eg:  perspective:500px; perspective-origin <1> transform: rotate(90deg)  意思是顺时针'旋转' 90度   ,  deg是单位度    可