CodeAshen's blog CodeAshen's blog
首页
  • Spring Framework

    • 《剖析Spring5核心原理》
    • 《Spring源码轻松学》
  • Spring Boot

    • Spring Boot 2.0深度实践
  • Spring Cloud

    • Spring Cloud
    • Spring Cloud Alibaba
  • RabbitMQ
  • RocketMQ
  • Kafka
  • MySQL8.0详解
  • Redis从入门到高可用
  • Elastic Stack
  • 操作系统
  • 计算机网络
  • 数据结构与算法
  • 云原生
  • Devops
  • 前端
  • 实用工具
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
  • Reference
GitHub (opens new window)

CodeAshen

后端界的小学生
首页
  • Spring Framework

    • 《剖析Spring5核心原理》
    • 《Spring源码轻松学》
  • Spring Boot

    • Spring Boot 2.0深度实践
  • Spring Cloud

    • Spring Cloud
    • Spring Cloud Alibaba
  • RabbitMQ
  • RocketMQ
  • Kafka
  • MySQL8.0详解
  • Redis从入门到高可用
  • Elastic Stack
  • 操作系统
  • 计算机网络
  • 数据结构与算法
  • 云原生
  • Devops
  • 前端
  • 实用工具
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
  • Reference
GitHub (opens new window)
  • CSS3浮动定位与背景样式

    • 01-浮动与定位
    • 02-边框与圆角
    • 03-背景与渐变
    • 04-2D与3D转换
      • 旋转形变
      • 缩放形变
      • 斜切形变
      • 位移形变
      • 3D旋转
      • 空间移动
      • (案例) 制作正方体
  • CSS动画

  • JS基础语法与表达式

  • 流程控制语句与数组

  • JS函数与DOM

  • 面向对象

  • 正则表达式

  • ES6基础入门

  • ES6语法扩展

  • Promise与Class

  • Module与Babel

  • 前端
  • CSS3浮动定位与背景样式
CodeAshen
2023-02-10
目录

04-2D与3D转换

# 2D形变

# 旋转形变

image-20220715031227586

  • 将 transform 属性的值设置为 rotate(),即可实现旋转变形。

  • 若角度为正,则顺时针方向旋转,否则逆时针方向旋转。

  • 默认围绕几何中间旋转,可以使用 transform-origin 属性指定旋转中心。

    2D形变都可以使用 transform-origin 来指定形变中心。

<!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>
        img{
            border: 1px solid #000;
        }
        .pic1 {
            /* 围绕几何中心顺时针30° */
            transform: rotate(30deg);
        }
        .pic2 {
            /* 围绕左上角逆时针30° */
            transform-origin: 0 0;
            transform: rotate(-30deg);
        }
    </style>
</head>
<body>
    <img src="images/goblin.png" class="pic1">
    <img src="images/goblin.png" class="pic2">
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# 缩放形变

image-20220715032044427

  • 将 transform 属性的值设置为 scale(),即可实现缩放变形。
  • 当数值小于 1 时,表示缩小元素;大于 1 表示放大元素。

# 斜切形变

image-20220715032351931

  • 将 transform 属性的值设置为 skew(),即可实现斜切变形

# 位移形变

image-20220715032445732

  • 将 transform 属性的值设置为 translate(),即可实现位移变形
  • 和相对定位非常像,位移变形也会 “老家留坑”,“形影分离”

# 3D形变

# 3D旋转

  • 将 transform 属性的值设置为 rotateX() 或者 rotateY(),即可实现绕横轴、纵轴旋转

    image-20220715033208692

  • perspective 属性,用来定义透视强度,可以理解为 “人眼到舞台的距离”,单位是 px。3D 旋转必须设置该属性,否则失效。

    image-20220715033517297

示例:

<!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>
        div {
            float: left;
            margin: 50px;
            width: 202px;
            height: 202px;
            border: 1px solid #000;
        }
        p {
            width: 200px;
            height: 200px;
            border: 1px solid #000;
            background-color: orange;
        }

        .box1 {
            /* 舞台必须设置该属性 */
            perspective: 300px;
        }
        .box1 p {
            /* 旋转度数 */
            transform: rotateX(30deg);
        }

        .box2 {
            perspective: 300px;
        }
        .box2 p {
            transform: rotateY(30deg);
        }

        .box3 {
            perspective: 300px;
        }
        .box3 p {
            transform: rotateX(30deg) rotateY(30deg);
        }
    </style>
</head>

<body>
    <div class="box1">
        <p></p>
    </div>
    <div class="box2">
        <p></p>
    </div>
    <div class="box3">
        <p></p>
    </div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

image-20220715034026227

# 空间移动

image-20220715034423477

  • 当元素进行 3D 旋转后,即可继续添加 translateX()、translateY()、translateZ() 属性让元素在空间进行移动
  • 一定记住,空间移动要添加在3D旋转之后

示例:

<!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>
        p {
            width: 200px;
            height: 200px;
            border: 1px solid #000;
            background-color: orange;
        }
        .box1 {
            width: 202px;
            height: 202px;
            border: 1px solid #000;
            margin: 50px auto;
            perspective: 300px;
        }
        .box1 p {
            transform: rotateX(30deg) translateX(100px) translateY(100px) translateZ(100px);
        }
    </style>
</head>

<body>
    <div class="box1">
        <p></p>
    </div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

image-20220715034808775

# (案例) 制作正方体

这是一个透视正方体,隧道的形式,黑色的线是基准正方形,上下左右面贴住基准面的边并垂直于它。前后面分别平行于基准面。

image-20220715035456885

代码中,外层 div 是面,六个 p 是正方体六个面。

<!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;
        }

        /* 基准面 */
        .box {
            width: 200px;
            height: 200px;
            border: 1px solid #000;
            margin: 100px auto;
            perspective: 300px;
            position: relative;
        }

        .box p {
            position: absolute;
            top: 0;
            left: 0;
            width: 200px;
            height: 200px;
        }

        /* 前面 */
        .box p:nth-child(1) {
            background-color: rgba(219, 56, 211, 0.486);
            transform: translateZ(100px);
        }
        /* 后面 */
        .box p:nth-child(3) {
            background-color: rgba(56, 219, 83, 0.486);
            transform: rotateX(180deg) translateZ(100px);
        }
        /* 顶面 */
        .box p:nth-child(2) {
            background-color: rgba(42, 128, 199, 0.486);
            transform: rotateX(90deg) translateZ(100px);
        }
        /* 底面 */
        .box p:nth-child(4) {
            background-color: rgba(213, 216, 32, 0.486);
            transform: rotateX(-90deg) translateZ(100px);
        }
        /* 左面 */
        .box p:nth-child(6) {
            background-color: rgba(119, 17, 236, 0.486);
            transform: rotateY(-90deg) translateZ(100px);
        }
        /* 右面 */
        .box p:nth-child(5) {
            background-color: rgba(236, 82, 102, 0.486);
            transform: rotateY(90deg) translateZ(100px);
        }
    </style>
</head>

<body>
    <div class="box">
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
    </div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
编辑 (opens new window)
上次更新: 2023/06/04, 12:34:19
03-背景与渐变
01-过渡与动画

← 03-背景与渐变 01-过渡与动画→

最近更新
01
第01章-RabbitMQ导学
02-10
02
第02章-入门RabbitMQ核心概念
02-10
03
第03章-RabbitMQ高级特性
02-10
更多文章>
Theme by Vdoing | Copyright © 2020-2023 CodeAshen | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式