css动画小练习——3D盒子

2022-03-21  本文已影响0人  moutory

前言

使用CSS动画实现将6个图片拼成正方体,并且旋转的练习。

一、html部分

HTML部分基本没啥好说的,就是一个Div元素里面放6个子元素,每个子元素存放1张图片。

<body>
    <div class="container">
        <div class="cube">
            <div class="box1"></div>
            <div class="box2"></div>
            <div class="box3"></div>
            <div class="box4"></div>
            <div class="box5"></div>
            <div class="box6"></div>
        </div>
    </div>
</body>

二、css部分

css部分主要分为2个部分:

<style>
        /* 设置视距 */
        html {
            perspective: 800px
        }
        .container{
            background-color: #fff;
            width: 600px;
            height: 600px;
            display: flex;
            justify-content: center;
        }
        .cube{
            margin-top: 100px;
            position: relative;
            width: 200px;
            height: 200px;
            /* 设置3d变形效果 */
            transform-style: preserve-3d;
            animation: box-rotate 10s infinite forwards linear;
        }
        .cube>div{
            width: 200px;
            height: 200px;
            position: absolute;
            opacity: .7;
        }
        .cube:hover{
            animation-play-state: paused;
        }
        .cube:hover > .box1{
            transform: rotateY(90deg) translateZ(120px);
        }
        .cube:hover > .box2{
            transform: rotateY(90deg) translateZ(-120px);
        }
        .cube:hover > .box3{
            transform: rotateX(90deg) translateZ(120px);
        }
        .cube:hover > .box4{
            transform: rotateX(90deg) translateZ(-120px);
        }
        .cube:hover > .box5{
            transform: translateZ(120px);
        }
        .cube:hover > .box6{
            transform: rotateZ(180deg) translateZ(-120px);
        }
        .box1{
            background-image: url(./image/fuchouzhe/1.jpg);
            transform: rotateY(90deg) translateZ(100px);
        }
        .box2{
            background-image: url(./image/fuchouzhe/2.jpg);
            transform: rotateY(90deg) translateZ(-100px);
        }
        .box3{
            background-image: url(./image/fuchouzhe/3.jpg);
            transform: rotateX(90deg) translateZ(100px);
        }
        .box4{
            background-image: url(./image/fuchouzhe/4.jpg);
            transform: rotateX(90deg) translateZ(-100px);
        }
        .box5{
            background-image: url(./image/fuchouzhe/5.jpg);
            transform: translateZ(100px);
        }
        .box6{
            background-image: url(./image/fuchouzhe/6.jpg);
            transform: rotateZ(180deg) translateZ(-100px);
        }
        @keyframes box-rotate {
            from{
                transform: rotateX(0) rotateZ(0);
            }
            to{
                transform: rotateX(1turn) rotateZ(1turn);
            }
        }
    </style>

三、效果图

cmbgj-mthf0.gif
上一篇下一篇

猜你喜欢

热点阅读