css3 3D变换和动画

时间:2022-10-23 13:25:49

3D变换和动画

  1. 建立3D空间,transform-style: preserve-3d
  2. perspective: 100px; 景深
  3. perspective-origin:center center -180px;  景深基点
  4. transform新增函数如下:
    • rotateX();
    • rotateY();
    • rotateZ();
    • translateZ();
    • scaleZ();
  5. 3d动画demo,只支持Webkit内核,可自行运行查看效果
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>无标题文档</title>
    <style>
    .wrap { width: 100px; height: 100px; -webkit-perspective: 200px; border: 5px solid pink; padding: 100px; margin: 200px auto 0; -webkit-transform: scale(2);}
    #box {width: 100px; height: 100px; position: relative; text-align: center; line-height: 100px; -webkit-transform-style: preserve-3d; transition: 3s; -webkit-transform-origin: center center -50px;}
    #box div {width: 100px; height: 100px; position: absolute; font-size: 50px; color: #FFF;}
    #box div:nth-of-type(1) {background-color: #F90; left: 0px; top: 0px;}
    #box div:nth-of-type(2) {background-color: #E08; left: 0px; top: -100px; -webkit-transform-origin: bottom; -webkit-transform: rotateX(90deg);}
    #box div:nth-of-type(3) {background-color: #A01; left: -100px; top: 0px; -webkit-transform-origin: right; -webkit-transform: rotateY(-90deg);}
    #box div:nth-of-type(4) {background-color: #CC0; left: 100px; top: 0px; -webkit-transform-origin: left; -webkit-transform: rotateY(90deg);}
    #box div:nth-of-type(5) {background-color: #D34; left: 0px; top: 100px; -webkit-transform-origin: top; -webkit-transform: rotateX(-90deg);}
    #box div:nth-of-type(6) {background-color: #000; left: 0px; top: 0px; -webkit-transform: translateZ(-100px) rotateX(180deg);}
    .wrap:hover #box{-webkit-transform: rotateX(360deg);}
    </style>
    <script>
    window.onload = function() {
    var oBox = document.getElementById("box");
    oBox.style.WebkitTransform = "rotateX(360deg)";
    addEnd(oBox, end1); function end1() {
    addEnd(oBox, end2);
    oBox.style.WebkitTransform = "rotateX(0deg)";
    } function end2() {
    addEnd(oBox, end3);
    oBox.style.WebkitTransform = "rotateY(360deg)";
    } function end3() {
    oBox.style.WebkitTransform = "rotateY(0deg)";
    } function addEnd(obj, fn) {
    obj.addEventListener('WebkitTransitionEnd', fn, false);
    obj.addEventListener('transitionend', fn, false);
    } function removeEnd(obj, fn) {
    obj.removeEventListener('WebkitTransitionEnd', fn, false);
    obj.removeEventListener('transitionend', fn, false);
    }
    };
    </script>
    </head>
    <body>
    <div class="wrap">
    <div id="box">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    </div>
    </div>
    </body>
    </html>