14、2D和3D效果

第一节 2D效果

1、CSS3过渡
2、2D变形

要实现这一点,必须规定两项内容:
1、规定您希望把效果添加到哪个CSS属性上
2、规定效果的时长

比方说,下面这个代码的动画效果,规定了效果的时长为2秒。

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        .box{width: 100px; height: 100px; background: #f00; transition: all 2s; }
        .box:hover {width: 200px; height: 200px; background: #0f0;}
    </style>
</head>
<body>
<div class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        .box{width: 100px; height: 100px; background: #f00; transition-property: width; transition-duration: 2s;}
        .box:hover {width: 200px; height: 200px; background: #0f0;}
    </style>
</head>
<body>
<div class="box"></div>
</body>
</html>
transition-property: width,height,background; transition-duration: 2s;

上面这一句代码的效果等同于下面这一句:

transition: all 2s;



cubic-bezier(n,n,n,n),贝塞尔曲线取值,可以利用以下网站去参考:
http://cubic-bezier.com/

延迟开始过渡效果。

总体上,transition的几个属性值,可以像以下的代码这样子写,来控制2D效果:

<style type="text/css">
        .box{width: 100px; height: 100px; background: #f00; transition: all 2s linear 1s;}
        .box:hover {width: 200px; height: 200px; background: #0f0;}
</style>



如下示例代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        .box{width: 100px; height: 100px; background: #f00; margin: 100px auto; transform: rotate(30deg);}
    </style>
</head>
<body>
<div class="box"></div>
</body>
</html>

不加transform前的位置和加了transform:translate()之后,位置上出现移动。

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        .box{width: 100px; height: 100px; background: #f00; margin: 100px auto; transform: translate(100px,100px);}
    </style>
</head>
<body>
<div class="box"></div>
</body>
</html>
特别注意:元素的缩放中心点是元素的中心位置。

简单例子如下:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        .box{width: 100px; height: 100px; background: #f00; margin: 100px auto; transform: scale(2,4);}
    </style>
</head>
<body>
<div class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        .box{width: 100px; height: 100px; background: #f00; margin: 100px auto; transform: skew(30deg,40deg);}
    </style>
</head>
<body>
<div class="box"></div>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        .box{width: 100px; height: 100px; background: #f00; margin: 100px auto; transform: rotate(-30deg);}
        .box2{width: 100px; height: 100px; background: #0f0; margin: 200px auto; transform-origin: left top; transform: rotate(-30deg); }
    </style>
</head>
<body>
<div class="box"></div>
<div class="box2"></div>
</body>
</html>

上面的box2是采用transform-origin:left top,对对应的正方形进行元素的中心点设置,之后进行变换的时候,依据这个中心点,进行相应的旋转等一系列变换。

下面这一个是对上面两种变换的进一步的优化,形成2D动画。

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        .box{width: 100px; height: 100px; background: #f00; margin: 100px auto; transition: all 1s;}
        .box:hover{transform: rotate(-30deg);}
        .box2{width: 100px; height: 100px; background: #0f0; margin: 200px auto; transform-origin: left top; transition: all 1s;}
        .box2:hover{transform: rotate(-30deg); }
    </style>
</head>
<body>
<div class="box"></div>
<div class="box2"></div>
</body>
</html>
下面是对整个2D效果的综合案例
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        div {width: 100px; height: 100px; border: 1px #f00 solid; margin: 100px auto; transition: all 2s;}
        div:nth-child(1):hover {transform:translateX(200px); }
        div:nth-child(2):hover {transform:rotate(90deg); }
        div:nth-child(3):hover {transform:scale(2,0.5); }
        div:nth-child(4):hover {transform:skew(30deg,30deg); }
        div:nth-child(5):hover {transform:translateX(200px) rotate(90deg) scale(2,0.5) skew(30deg,30deg); }
        div:nth-child(6):hover {transform:rotate(90deg) scale(2,0.5) skew(30deg,30deg) translateX(200px) ; }
        div:nth-child(7):hover {transform: scale(2,0.5) rotate(90deg) skew(30deg,30deg) translateX(200px) ; }
    </style>
</head>
<body>
<div>2D平移</div>
<div>2D旋转</div>
<div>2D缩放</div>
<div>2D扭曲</div>
<div>2D综合</div>
<div>2D综合2</div>
<div>2D综合3</div>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • CSS3的变形transform、过渡transition、动画animation学习 学习CSS3动画anima...
    yichen_china阅读 3,842评论 0 1
  • 看了很多视频、文章,最后却通通忘记了,别人的知识依旧是别人的,自己却什么都没获得。此系列文章旨在加深自己的印象,因...
    DCbryant阅读 1,896评论 0 4
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 2,337评论 0 11
  • W3C标准中对css3的transition这是样描述的:“css的transition允许css的属性值在一定的...
    青春前行阅读 1,433评论 0 5
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    wzhiq896阅读 1,805评论 0 2