美文网首页
css3用transform实现正方体效果

css3用transform实现正方体效果

作者: 星星藏进黑夜 | 来源:发表于2023-05-25 18:43 被阅读0次

    核心代码

    transform-style: preserve-3d;
    transform属性

    效果图

    image.png
    image.png

    代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            body{
                /* perspective: 800px; */
            }
            .box{
                width: 100px;
                height: 100px;
                position: absolute;
                top: 100px;
                left: 0;
                right: 0;
                margin: auto;
                transform-style: preserve-3d;
                transform: rotateX(20deg) rotateY(20deg) rotateZ(0deg);
                animation: rot 1000s linear;
            }
            @keyframes rot{
                0% {
                    /* transform: rotateX(20deg) rotateY(20deg) rotateZ(0deg); */
                }
                100% {
                    transform: rotateX(20deg) rotateY(60000deg) rotateZ(0deg);
                }
            }
            .box-item{
                width: 100px;
                height: 100px;
                position: absolute;
                top: 0;
                left: 0;
                border: 1px solid pink;
                opacity: .3;
                transition: .6s;
            }
            .b1{
                transform: translateX(-50px) rotateY(90deg);
                background: rgb(255, 131, 131);
            }
            .box:hover .b1{
                transform: translateX(-50px) rotateY(90deg) translateZ(-30px);
                background: rgb(255, 131, 131);
            }
            .b2{
                transform: translateX(50px) rotateY(90deg);
                background: #fffd6a;
            }
            .box:hover .b2{
                transform: translateX(50px) rotateY(90deg) translateZ(30px);
                background: #fffd6a;
            }
            .b3{
                transform: translateZ(50px);
                background: #ff0000;
            }
            .box:hover .b3{
                transform: translateZ(50px) translateZ(30px);
                background: #ff0000;
            }
            .b4{
                transform: translateZ(-50px);
                background: #00e5ff;
            }
            .box:hover .b4{
                transform: translateZ(-50px) translateZ(-30px);
                background: #00e5ff;
            }
            .b5{
                transform: translateY(-50px) rotateX(90deg);
                background: #0011ff;
            }
            .box:hover .b5{
                transform: translateY(-50px) rotateX(90deg) translateZ(30px);
                background: #0011ff;
            }
            .b6{
                transform: translateY(50px) rotateX(90deg);
                background: #ff6af3;
            }
            .box:hover .b6{
                transform: translateY(50px) rotateX(90deg) translateZ(-30px);
                background: #ff6af3;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <div class="box-item b1"></div>
            <div class="box-item b2"></div>
            <div class="box-item b3"></div>
            <div class="box-item b4"></div>
            <div class="box-item b5"></div>
            <div class="box-item b6"></div>
        </div>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:css3用transform实现正方体效果

          本文链接:https://www.haomeiwen.com/subject/arbhedtx.html