利用css的3d动画写一个立方体

总结:1.transform-style: preserve-3d; 要写在3d动画的父级上否则不会生效;

   2.每个面在做变换时更换他们的基点再变换

   3.正方体转动时以它的中心转 得把基点设置为x轴y轴z轴的中心

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #warp{
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
            width: 300px;
            height: 300px;
            border: 1px solid black;
            perspective: 200px; /*设置景深该css具有后代继承‘决定用户的肉眼距离屏幕的水平距离!使3D变换的元素具有近大远小的感觉’*/

        }
        .box{
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
            width: 100px;
            height: 100px;
            transform-style: preserve-3d;   /*搭建3d舞台作用于直接子元素(多个舞台会产生叠加效果)*/
            transform-origin: center center -50px; /*正方体的基点*/
            transition: 3s;
        }
        #warp .box >div{
            position: absolute;
             background-color: rgba(123,253,24,0.7);
            width: 100px;
            height: 100px;
            line-height: 100px;
            text-align: center;

        }
        #warp .box >div:nth-child(1){
            z-index: 1;
        }
        #warp .box >div:nth-child(2){
            transform: translateZ(-100px) rotateY(180deg);
        }
        #warp .box >div:nth-child(3){
            transform-origin: top;
            transform: rotateX(270deg);
        }
        #warp .box >div:nth-child(4){
            transform-origin: bottom;
            transform: rotateX(-270deg);}
        #warp .box >div:nth-child(5){
            transform-origin: left;
            transform: rotateY(90deg);
        }
        #warp .box >div:nth-child(6){
            transform-origin: right;
            transform: rotateY(-90deg);
        }
        #warp .box:hover{
            transform: rotate3d(1,2,3,720deg);
        }
    </style>
</head>
<body>
<div id="warp">
    <div class="box">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</div>
</body>
</html>
原文地址:https://www.cnblogs.com/threeyou/p/13521106.html