3D景深探究

参照CSS3景深、三维变换属性及旋转三维立方体的实现

        .stage1 {
            width: 200px;
            height: 200px;
            border: 1px solid black;
            margin: 100px auto;
            perspective: 500px;
            /* perspective-origin: 10px 10px;//舞台元素的中心位置 默认perspective-origin:  50% 50%; */
        }

        .stage1 .demo {
            width: 200px;
            height: 200px;
            background-color: orangered;
            transform: rotateX(45deg);
        }

        ul {
            /*调整ul标签的样式,取消内边距、外边距,和“点”样式*/
            padding: 0;
            margin: 0;
            list-style-type: none;
        }

        .stage {
            /*设置舞台元素在屏幕居中,设置合适的景深大小*/
            position: relative;
            width: 800px;
            height: 800px;
            margin: 100px auto;
            perspective: 800px;
        }

        @keyframes move {

            /*设置动画关键帧*/
            0% {
                transform: rotateX(0deg);
            }

            25% {
                transform: rotateX(180deg);
            }

            50% {
                transform: rotateX(360deg) rotateY(0deg);
            }

            75% {
                transform: rotateX(360deg) rotateY(180deg);
            }

            100% {
                transform: rotateX(360deg) rotateY(360deg);
            }
        }

        .stage .three-d-box {
            /*动画容器居中在舞台元素中间*/
            width: 200px;
            height: 200px;
            position: absolute;
            left: 50%;
            top: 50%;
            margin: -100px 0 0 -100px;
            transform-style: preserve-3d;
            /*设置3D属性让子元素三维空间呈现*/
            animation: move 3s linear infinite;
            /*设置动画*/

        }

        .stage .three-d-box>li {
            /*设置动画子元素公共属性*/
            position: absolute;
            width: 200px;
            height: 200px;
            left: 0;
            top: 0;
            font-size: 50px;
            line-height: 200px;
            text-align: center;
            opacity: 0.5;
        }

        /*为了保证我们对立方体位置的控制,我们需要让动画容器在立方体的中间位置*/
        .stage .three-d-box>li:nth-child(1) {
            background-color: red;
            transform: translateZ(-100px);
        }

        .stage .three-d-box>li:nth-child(2) {
            background-color: yellow;
            transform: translateZ(100px);
        }

        .stage .three-d-box>li:nth-child(3) {
            background-color: blue;
            transform: rotateX(90deg) translateZ(100px);
        }

        .stage .three-d-box>li:nth-child(4) {
            background-color: green;
            transform: rotateX(-90deg) translateZ(100px);
        }

        .stage .three-d-box>li:nth-child(5) {
            background-color: black;
            transform: rotateY(90deg) translateZ(100px);
        }

        .stage .three-d-box>li:nth-child(6) {
            background-color: purple;
            transform: rotateY(-90deg) translateZ(100px);
        }
    <div class="stage1">
        <div class="demo">1</div>
    </div>
    <!--舞台元素,视角所在-->
    <div class="stage">
        <!--动画容器,通过它来控制整个立方体-->
        <ul class="three-d-box">
            <li></li>
            <!--动画元素,立方体的六个面-->
            <li></li>
            <li></li>
            <li>绿</li>
            <li></li>
            <li></li>
        </ul>
    </div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值