3D box

Thinking: After moving element translate, transformation does not modify the source element itself. It is possible to translate the same distance to each face, then the faces is rotated, a cube can be obtained.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #wrap {
            width: 400px;
            height: 400px;
            border: 1px solid black;
            margin: 100px auto;
            transform-style: preserve-3d;
            /* 注意:景深需要加在需要动画的父级 */
            perspective: 1200px;
            perspective-origin: center center center;
        }
        #box {
            padding: 100px;
            transform-style: preserve-3d;
            transition: 5s linear;
            
        }
        #box span{
            display: block;
            width: 200px;
            height: 200px;
            position: absolute;
            top: 100px;
            left: 100px;
            /* background: red; */
            font: 80px/200px "宋体";
            text-align: center;
            opacity: .5;
        }
        #box span:nth-of-type(1){
            transform: rotateY(90deg) translateZ(-100px);
            background: red;
        }
        #box span:nth-of-type(2){
            transform: rotateY(180deg) translateZ(-100px);
            background: blue;
        }
        #box span:nth-of-type(3){
            transform: rotateY(270deg) translateZ(-100px);
            background: green;
        }
        #box span:nth-of-type(4){
            transform: rotateY(360deg) translateZ(-100px);
            background: peru;
        }
        #box span:nth-of-type(5){
            transform: rotateX(90deg) translateZ(-100px);
            background: peru;
        }
        #box span:nth-of-type(6){
            transform: rotateX(-90deg) translateZ(-100px);
            background: peru;
        }
        #wrap:hover #box {
            transform: rotateY(360deg);
        }
    </style>
</head>
<body>
    <div id="wrap">
        <div id="box">
            <span>1</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
        </div>
    </div>
</body>
</html>

Published 95 original articles · won praise 115 · views 120 000 +

Guess you like

Origin blog.csdn.net/qq_34569497/article/details/99635990