CSS之3D转换

三维坐标系

三维坐标系其实就是指立体空间,立体空间是由3个轴共同组成的。

x轴:水平向右注意:x右边是正值,左边是负值

y轴:垂直向下注意:y下面是正值,上面是负值

z轴:垂直屏幕注意:往外面是正值,往里面是负值

3D移动 translate3d

3D移动在2D移动的基础上多加了一个可以移动的方向,就是z轴方向,

translform:translateX(100px):仅仅是在x轴上移动

translform:translateY(100px):仅仅是在Y轴上移动

translform:translateZ(100px):仅仅是在Z轴上移动(注意:translateZ-般用px单位)

transform:translate3d(x,y,z):其中x、y、z分别指要移动的轴的方向的距离 (xyz是不能省略的,如果没有就写)

透视 perspective

在2D平面产生近大远小视觉立体,但是只是效果二维的

如果想要在网页产生3D效果需要透视(理解成3D物体投影在2D平面内)。模拟人类的视觉位置,可认为安排一只眼睛去看

透视我们也称为视距:视距就是人的眼睛到屏幕的距离距离视觉点越近的在电脑平面成像越大,越远成像越小透视的单位是像素

透视写在被观察元素的父盒子上面的

d:就是视距,视距就是一个距离人的眼睛到屏幕的距离。

z:就是 z轴,物体距离屏幕的距离,z轴越大(正值 )我们看到的物体就越大

3D旋转 rotate3d

3D旋转指可以让元素在三维平面内沿着x轴,y轴,z轴或者自定义轴进行旋转语法

transform:rotateX(45deg):沿着x轴正方向旋转 45度

transform:rotateY(45deg):沿着y轴正方向旋转 45deg

transform:rotateZ(45deg):沿着Z轴正方向旋转 45deg

transform:rotate3d(xy,z,deg):沿着自定义轴旋转 deg为角度(了解即可)

transform:rotate3d(x,y,z,deg):沿着自定义轴旋转 deg为角度(了解即可)xyz是表示旋转轴的矢量,是标示你是否希望沿着该轴旋转,最后一个标示旋转的角度

3D呈现 transfrom-style

控制子元素是否开启三维立体环境。

transform-style:flat子元素不开启3d立体空间 默认的

transform-style:pfeserve-3d;子元素开启立体空间

代码写给父级,但是影响的是子盒子

案例一:

html 复制代码
<!DOCTYPE html>
<html lang="en">

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

        .box {
            position: relative;
            width: 200px;
            height: 200px;
            margin: 100px auto;
            transition: all 2s;
            transform-style: preserve-3d;
        }


        .box div {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: pink;
        }

        .box:hover {
            transform: rotateY(60deg);
        }

        .box div:last-child {
            background-color: purple;
            transform: rotateX(60deg);
        }
    </style>
</head>

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

</html>

案例二:两面盒子翻转

html 复制代码
<!DOCTYPE html>
<html lang="en">

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

        .box {
            position: relative;
            width: 100px;
            height: 100px;
            margin: 100px auto;
            transition: all .4s;
            transform-style: preserve-3d;
        }

        .front,
        .back {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            text-align: center;
            line-height: 100px;
            font-size: 20px;
            color: #fff;
            backface-visibility: hidden;
        }

        .front {
            background-color: pink;
            z-index: 1;
        }

        .back {
            background-color: purple;
            transform: rotateY(180deg);
        }

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

<body>
    <div class="box">
        <div class="front">正面</div>
        <div class="back">反面</div>
    </div>
</body>

</html>

案例三:3D导航栏

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul {
            margin: 100px;
        }

        ul li {
            float: left;
            width: 120px;
            height: 35px;
            list-style: none;
            perspective: 500px;
            margin: 10px;
        }

        .box {
            position: relative;
            width: 100%;
            height: 100%;
            transform-style: preserve-3d;
            transition: all .3s;
        }

        .box:hover {
            transform: rotateX(90deg);
        }

        .front,
        .bottom {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            text-align: center;
            line-height: 35px;
        }

        .front {
            background-color: pink;
            z-index: 1;
            transform: translateZ(17.5px);
        }

        .bottom {
            background-color: purple;
            transform: translateY(17.5px) rotateX(-90deg);
        }
    </style>
</head>

<body>
    <ul>
        <li>
            <div class="box">
                <div class="front">导航</div>
                <div class="bottom">选项</div>
            </div>
        </li>
        <li>
            <div class="box">
                <div class="front">导航</div>
                <div class="bottom">选项</div>
            </div>
        </li>
        <li>
            <div class="box">
                <div class="front">导航</div>
                <div class="bottom">选项</div>
            </div>
        </li>
        <li>
            <div class="box">
                <div class="front">导航</div>
                <div class="bottom">选项</div>
            </div>
        </li>
    </ul>
</body>

</html>

案例四:旋转木马

html 复制代码
<!DOCTYPE html>
<html lang="en">

<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: rotate 10s linear infinite;
            background: url(images/dog.jpg);
        }

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

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

            100% {
                transform: rotateY(360deg);
            }
        }

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

        section div:nth-child(1) {
            transform: translateZ(300px);
        }

        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>1</div>
        <div>2</div>
        <div>3</div>
        <div></div>
        <div></div>
        <div></div>
    </section>
</body>

</html>
相关推荐
LawrenceLan4 分钟前
Flutter 零基础入门(二十六):StatefulWidget 与状态更新 setState
开发语言·前端·flutter·dart
秋秋小事24 分钟前
TypeScript 模版字面量与类型操作
前端·typescript
2401_892000521 小时前
Flutter for OpenHarmony 猫咪管家App实战 - 添加提醒实现
前端·javascript·flutter
Yolanda941 小时前
【项目经验】vue h5移动端禁止缩放
前端·javascript·vue.js
广州华水科技2 小时前
单北斗GNSS形变监测一体机在基础设施安全中的应用与技术优势
前端
EndingCoder2 小时前
案例研究:从 JavaScript 迁移到 TypeScript
开发语言·前端·javascript·性能优化·typescript
阿珊和她的猫4 小时前
React 路由:构建单页面应用的导航系统
前端·react.js·状态模式
Amumu121384 小时前
Vue脚手架(二)
前端·javascript·vue.js
花间相见4 小时前
【LangChain】—— Prompt、Model、Chain与多模型执行链
前端·langchain·prompt
lichenyang4535 小时前
从零开始构建 React 文档系统 - 完整实现指南
前端·javascript·react.js