CSS transform3D

rotateX():正值向上翻转

rotateY():正值向右翻转

translateZ():正值向前,负值向右

scaleZ():立体元素的厚度

3D相关属性

perspective:离屏幕多远的距离去观察元素,值越大幅度就越小。

perspective-origin:景深-基点位置,观察元素的角度。

transform-origin:x、y、z

transform-style:3D空间

  • flat(默认值,2D)

  • preserve-3d(3D,产生一个三维空间)

backface-visibility:背面隐藏

  • hidden

  • visible

实现3D立方体

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>3D立方体 By ToDo Blog</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }
        .box {
            width: 300px;
            height: 300px;
            border: 1px black solid;
            margin: 30px auto;
            perspective: 200px;
        }
        .box ul {
            width: 100px;
            height: 100px;
            margin: 100px;
            position: relative;
            transform-style: preserve-3d;
            transition: 2s;
            transform-origin: center center -50px;
        }
        .box ul li {
            width: 100px;
            height: 100px;
            position: absolute;
            line-height: 100px;
            text-align: center;
            color: #ffffff;
            font-size: 26px;
        }
        .box ul li:nth-child(1) {
            background: red;
            left: 0;
            top: 0;
        }
        .box ul li:nth-child(2) {
            background: blue;
            left: 100px;
            top: 0;
            transform-origin: left;
            transform: rotateY(90deg);
        }
        .box ul li:nth-child(3) {
            background: yellow;
            left: -100px;
            top: 0;
            transform-origin: right;
            transform: rotateY(-90deg);
        }
        .box ul li:nth-child(4) {
            background: green;
            left: 0;
            top: -100px;
            transform-origin: bottom;
            transform: rotateX(90deg);
        }
        .box ul li:nth-child(5) {
            background: pink;
            left: 0;
            top: 100px;
            transform-origin: top;
            transform: rotateX(-90deg);
        }
        .box ul li:nth-child(6) {
            background: gray;
            left: 0;
            top: 0;
            transform: translateZ(-100px) rotateY(180deg);
        }
        .box:hover ul {
            transform: rotateY(360deg);
        }
    </style>
</head>
<body>
    <div class="box">
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
        </ul>
    </div>
</body>
</html>

实现3D旋转木马效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>旋转木马 By ToDo Blog</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }
        img {
            display: block;
        }
        .parent {
            width: 600px;
            height: 300px;
            border: 1px black solid;
            margin: 100px auto;
            perspective: 700px;
        }
        .parent ul {
            width: 128px;
            height: 94px;
            margin: 100px auto;
            position: relative;
            transform-style: preserve-3d;
            transition: 2s;
        }
        .parent ul li {
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
        }
        .parent ul li:nth-child(1) {
            transform: rotateY(0) translateZ(200px);
        }
        .parent ul li:nth-child(2) {
            transform: rotateY(60deg) translateZ(200px);
        }
        .parent ul li:nth-child(3) {
            transform: rotateY(120deg) translateZ(200px);
        }
        .parent ul li:nth-child(4) {
            transform: rotateY(180deg) translateZ(200px);
        }
        .parent ul li:nth-child(5) {
            transform: rotateY(240deg) translateZ(200px);
        }
        .parent ul li:nth-child(6) {
            transform: rotateY(300deg) translateZ(200px);
        }
        .parent:hover ul {
            transform: rotateY(360deg);
        }
    </style>
</head>
<body>
    <!-- 图片素材源于网络,如有侵权请联系 -->
    <div class="parent">
        <ul>
            <li>
                <img src="./images/1.jpg" alt="img">
            </li>
            <li>
                <img src="./images/2.jpg" alt="img">
            </li>
            <li>
                <img src="./images/3.jpg" alt="img">
            </li>
            <li>
                <img src="./images/4.jpg" alt="img">
            </li>
            <li>
                <img src="./images/5.jpg" alt="img">
            </li>
            <li>
                <img src="./images/6.jpg" alt="img">
            </li>
        </ul>
    </div>
</body>
</html>

此处内容需要评论回复后方可阅读。

3D图片翻转效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>3D图片翻转效果 By ToDo Blog</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        img {
            display: block;
        }
        .parent {
            width: 1110px;
            height: 476px;
            margin: 100px auto;
            position: relative;
            perspective: 800px;
        }
        .parent div {
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            backface-visibility: hidden;
            transition: 1s;
        }
        .parent div:first-child {
            transform: rotateY(0);
        }
        .parent div:last-child {
            transform: rotateY(-180deg);
        }
        .parent:hover div:first-child {
            transform: rotateY(180deg);
        }
        .parent:hover div:last-child {
            transform: rotateY(0);
        }
    </style>
</head>
<body>
    <div class="parent">
        <div>
            <img src="http://www.dotaoke.cn/img/py_banner.jpg" alt="Python">
        </div>
        <div>
            <img src="http://www.dotaoke.cn/img/go_banner.jpg" alt="GoLang">
        </div>
    </div>
</body>
</html>

不起眼的小广告

do淘客