HTML+CSS利用3D转换translate/rotate 实现旋转木马/翻转盒子/3D导航栏

1,485 阅读1分钟

这是我参与8月更文挑战的第28天,活动详情查看:8月更文挑战

上一篇文章中我们学习了一些CSS 3D特效的一些理论知识,下面我们就来实战一下,实现一个3D导航栏,翻转盒子和旋转木马的特效

 案例一 实现两个翻转的盒子

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>3d demo</title>
    <style>
        body{
            perspective: 350px;
        }
        .box {
            position: relative;
            width: 200px;
            height: 200px;
            margin: 100px auto;
            transform-style: preserve-3d;
            transition: all 0.4s;
        }

        .box div {
            position: absolute;
            width: 200px;
            height: 200px;
            line-height: 200px;
            text-align: center;
            border-radius: 50%;
        }

        .box div:first-child {
            background-color: pink;
            z-index: 1;
        }

        .box div:last-child {
            background-color: green;
            transform: rotateY(180deg);
        }

        .box:hover{
            transform: rotateY(180deg);
        }
    </style>
</head>

<body>
    <div class="box">
        <div>Hello</div>
        <div>world</div>
    </div>
</body>

</html>

案例二 3D导航栏

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>3d demo</title>
    <style>
        .box {
            position: relative;           
            transform-style: preserve-3d;
            transition: all 0.4s;
        }

        .box div {
            width: 200px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            background-color:pink;
        }

        .box .shn {
            background-color: green;
           transform: translateY(-25px) translateZ(-25px) rotateX(-90deg)
        }

        .box:hover{
            transform: translateY(-50px) rotateX(90deg);
        }

        li{
            float: left;
            list-style: none;
        }
    </style>
</head>

<body>
<ul>
<li>
    <div class="box">
        <div>导航一</div>
        <div class="shn">导航一</div>
    </div>
</li>
<li>
    <div class="box">
        <div>导航二</div>
        <div class="shn">导航二</div>
    </div>
</li>
<li>
    <div class="box">
        <div>导航三</div>
        <div class="shn">导航三</div>
    </div>
</li>
</ul>
</body>

</html>

案例三 旋转木马

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>旋转木马</title>
    <style>
        body {
            perspective: 1000px;
        }

        section {
            position: relative;
            width: 300px;
            height: 200px;
            margin: 200px auto;
            transform-style: preserve-3d;
            animation: rotate 10s linear infinite;
            background: url(caoyaolu.jpg) no-repeat center;
        }

        section div {
            position: absolute;
            width: 100%;
            height: 100%;
            background: url(timg.jfif) no-repeat;
        }

        section:hover {
            animation-play-state: paused;
        }

        section div:first-child {
            transform: translateZ(400px);
        }

        section div:nth-child(2) {
            transform: rotateY(60deg) translateZ(400px);
        }

        section div:nth-child(3) {
            transform: rotateY(120deg) translateZ(400px);
        }

        section div:nth-child(4) {
            transform: rotateY(180deg) translateZ(400px);
        }

        section div:nth-child(5) {
            transform: rotateY(240deg) translateZ(400px);
        }

        section div:nth-child(6) {
            transform: rotateY(300deg) translateZ(400px);
        }

        @keyframes rotate {
            0% {
                transform: rotateY(0deg);
            }

            100% {
                transform: rotateY(100deg);
            }
        }
    </style>
</head>

<body>
    <section>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </section>
</body>

</html>