CSS实现3D旋转木马效果

码农公社  210.net.cn   210是何含义?10月24日是程序员节,1024 = 210、210既 210 之意。


    3D 旋转木马是CSS中常见的特效之一,旋转木马可以有多种方法实现,这里我使用纯CSS实现这种动画的效果。

 

    简要介绍一下重点

transform: rotateY(60deg) translateZ(300px);
这是必须先旋转后 沿着z轴移动,不然会错乱,translateZ是沿着Z轴移动,其值越大,我们看见的图像就越大。


<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            perspective: 1000px;
        }

        section {
            position: relative;
            width: 300px;
            height: 200px;
            margin: 150px auto;
            transform-style: preserve-3d;
            animation: roate 10s linear infinite;
        }

        section div {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            /* background: url(media/dog.jpg) no-repeat; */
            background-color: yellow;
        }

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

        @keyframes roate {
            0% {
                transform: rotateY(0);
            }
            100% {
                transform: rotateY(360deg);
            }
        }

        section div:nth-child(1) {
            transform: rotateY(0) translateZ(300px);
            /* 可以添加自己想添加的图片 这里用背景色pink代替 下面都一样*/
            background-color: pink;
        }

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

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

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

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

        section div:nth-child(6) {
            transform: rotateY(300deg) translateZ(300px);
        }
    </style>
</head>

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

</html>


评论