avatar

CSS3高级

本章介绍2d,3d转换和动画

思维导图

transform(2D)

移动盒子translate ★

格式1 transform:translate(x,y)

格式2 transform:translateX(x)

格式3 transform:translateY(y)

括号中写X轴和Y轴的偏移量,在移动的时候相对自身原来的位置,对其他元素没有影响,x和y的单位如果是50%,相对的是自身的百分之50。

注意:margin-left,或者left…..这些样式写%,相对的是父盒子

应用场景

1.面试中盒子居中问题,如何让一个块元素居中,给要居中的块元素添加样式

1
2
3
4
5
/*移动父盒子的一半*/
margin-left:50%;
margin-top:50%;
/*移动自身的一半*/
transform:translate(-50%,-50%);

2.在网页上鼠标移动到盒子上,盒子向上移动一点点。

1623287226789

旋转盒子rotate

格式 transform:rotate(11deg) deg表示度,旋转多少度

功能

让盒子在2D屏幕旋转

案例:鼠标移动到图片上旋转图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
/* 需求:鼠标移动到图片上旋转图片 */
img {
transition: all 0.5s;
}
img:hover {
transform: rotate(45deg);
}
</style>
</head>
<body>
<img src="./media/pic.jpg" alt="" />
</body>
</html>

素材

1623287226789

旋转中心点transform-origin

格式 transform-origin:X,Y 确定旋转点,如果想围绕盒子中心旋转 50%,50%(默认)

除了像素,%,还可以设置方位名词,比如左中右,上中下

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
div {
/* 1 */
width: 100px;
height: 100px;
background-color: brown;

/* 5 */
overflow: hidden;
}
div::before {
content: "";
display: block;
width: 100px;
height: 100px;
background-color: yellow;

/* 2 */
transform: rotate(180deg);
transform-origin: left bottom;

/* 4 */
transition: all 0.5s;
}

/* 3 */
div:hover::before {
transform: rotate(0deg);
}
</style>
</head>
<body>
<div></div>
</body>
</html>

缩放scale★

格式transform: scale(x,y) x和y不需要加单位,表示多少倍,默认围绕中心点缩放。并且缩放后不会影响其他盒子

应用场景

商品列表中,鼠标移动到商品上,商品的图片会放大

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
div {
/* 1 */
width: 100px;
height: 100px;
border: 2px solid black;
/* 4 */
overflow: hidden;
}
/* 3 */
div:hover img {
transform: scale(1.2, 1.2);
}
img {
/* 2 */
width: 100%;
height: 100%;
/* 5 */
transition: all 0.5s;
}
</style>
</head>
<body>
<div>
<img src="./media/pic.jpg" alt="" />
</div>
</body>
</html>

简写格式

  1. 知识要点

    • 同时使用多个转换,其格式为 transform: translate() rotate() scale()
    • 顺序会影响到转换的效果(先旋转会改变坐标轴方向)
    • 但我们同时有位置或者其他属性的时候,要将位移放到最前面
  2. 代码演示

    1
    2
    3
    div:hover {
    transform: translate(200px, 0) rotate(360deg) scale(1.2)
    }

案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
div {
width: 100px;
height: 100px;
background-color: red;
}

div:hover {
transform: translate(100px, 100px) rotate(90deg) scale(2);
}
</style>
</head>
<body>
<div></div>
</body>
</html>

动画(animation)

什么是动画

  • 动画是 CSS3 中最具颠覆性的特征之一,可通过设置多个节点来精确的控制一个或者一组动画,从而实现复杂的动画效果

动画的基本使用

  • 先定义动画
  • 在调用定义好的动画

语法格式(定义动画)★

1
2
3
4
5
6
7
8
@keyframes 动画名称 {
0% {
width: 100px;
}
100% {
width: 200px
}
}

语法格式(使用动画)

1
2
3
4
5
6
div {
/* 调用动画 */
animation-name: 动画名称;
/* 持续时间 */
animation-duration: 持续时间;
}

动画序列

  • 0% 是动画的开始,100 % 是动画的完成,这样的规则就是动画序列
  • 在 @keyframs 中规定某项 CSS 样式,就由创建当前样式逐渐改为新样式的动画效果
  • 动画是使元素从一个样式逐渐变化为另一个样式的效果,可以改变任意多的样式任意多的次数
  • 用百分比来规定变化发生的时间,或用 fromto,等同于 0% 和 100%
  1. 代码演示

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <style>
    div {
    width: 100px;
    height: 100px;
    background-color: aquamarine;
    animation-name: move;
    animation-duration: 0.5s;
    }

    @keyframes move{
    0% {
    transform: translate(0px)
    }
    100% {
    transform: translate(500px, 0)
    }
    }
    </style>

动画常见属性

  1. 常见的属性

    class="lazyload" data-src="https://gitee.com/haoyongliang/resources/raw/master/images/%E5%89%8D%E7%AB%AF/css/css3%E9%AB%98%E7%BA%A7/animationcanshu.png"
  1. 代码演示

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    div {
    width: 100px;
    height: 100px;
    background-color: aquamarine;
    /* 动画名称 */
    animation-name: move;
    /* 动画花费时长 */
    animation-duration: 2s;
    /* 动画速度曲线 */
    animation-timing-function: ease-in-out;
    /* 动画等待多长时间执行 */
    animation-delay: 2s;
    /* 规定动画播放次数 infinite: 无限循环 */
    animation-iteration-count: infinite;
    /* 是否逆行播放 */
    animation-direction: alternate;
    /* 动画结束之后的状态 */
    animation-fill-mode: forwards;
    }

    div:hover {
    /* 规定动画是否暂停或者播放 */
    animation-play-state: paused;
    }

动画简写方式

  1. 动画简写方式

    1
    2
    /* animation: 动画名称 持续时间 运动曲线 何时开始 播放次数 是否反方向 起始与结束状态 */
    animation: name duration timing-function delay iteration-count direction fill-mode
  2. 知识要点

    • 简写属性里面不包含 animation-paly-state
    • 暂停动画 animation-paly-state: paused; 经常和鼠标经过等其他配合使用
    • 要想动画走回来,而不是直接调回来:animation-direction: alternate
    • 盒子动画结束后,停在结束位置:animation-fill-mode: forwards
  3. 代码演示

    1
    animation: move 2s linear 1s infinite alternate forwards;

速度曲线细节

animation-timing-function: 规定动画的速度曲线,默认是ease

class="lazyload" data-src="https://gitee.com/haoyongliang/resources/raw/master/images/%E5%89%8D%E7%AB%AF/css/css3%E9%AB%98%E7%BA%A7/steps.png"

动画常用缩写★

div{animation: xxx 3s infinite } 动画每次播放3S 一直重复播放
div{animation:xxx 3s forwards} 动画播放完毕后保持结束位置
div{animation: xxx 3s 1s infinite } 动画首次1S后播放,之后循环播放,每次播放时间3S

步进steps()

代码演示 打字机效果

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
div {
/* 2 */
border: 1px solid black;
width: 16px;
height: 20px;
overflow: hidden;

white-space: nowrap;

/* 4 */
animation: binbin 12s steps(23) forwards;
}

/* 3动画的功能:修改盒子的宽度 */
@keyframes binbin {
from {
}
to {
width: 368px;
}
}
</style>
</head>
<body>
<!-- 1 -->
<div>请勿在屏幕广播状态下登陆,登陆时请注意密码保护</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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.bear {
width: 200px;
height: 100px;
border: 1px solid blue;
background: cadetblue url(./bear.png) no-repeat 0 0;

animation: move 1s steps(8) infinite, xx 2s forwards;
}

@keyframes move {
0% {
}
100% {
background-position: -1600px 0;
}
}
@keyframes xx {
0% {
}
100% {
margin-left: 50%;
transform: translateX(-50%);
}
}
</style>
</head>
<body>
<div class="bear"></div>
</body>
</html>

素材

transform(3d)

移动盒子 translate3d

格式 transform: translate3d(x,y,z)

transform:translateZ(xx)

在z轴移动盒子后看不到效果,需要找到当前盒子的父盒子,设置透视perspective:200px 如果透视距离过近,可能你的盒子就跑出屏幕了,看不到了,这时可以将透视距离设置的大一些。

认识 3D 转换

  1. 3D 的特点

    • 近大远小
    • 物体和面遮挡不可见
  2. 三维坐标系

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

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

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

      class="lazyload" data-src="https://gitee.com/haoyongliang/resources/raw/master/images/%E5%89%8D%E7%AB%AF/css/css3%E9%AB%98%E7%BA%A7/sanwei.png"

3D 转换

  1. 3D 转换知识要点

    • 3D 位移:translate3d(x, y, z)
    • 3D 旋转:rotate3d(x, y, z)
    • 透视:perspctive
    • 3D呈现 transfrom-style
  2. 3D 移动 translate3d

    • 3D 移动就是在 2D 移动的基础上多加了一个可以移动的方向,就是 z 轴方向
    • transform: translateX(100px):仅仅是在 x 轴上移动
    • transform: translateY(100px):仅仅是在 y 轴上移动
    • transform: translateZ(100px):仅仅是在 z 轴上移动
    • transform: translate3d(x, y, z):其中x、y、z 分别指要移动的轴的方向的距离
    • 注意:x, y, z 对应的值不能省略,不需要填写用 0 进行填充
  3. 语法

    1
    transform: translate3d(x, y, z)
  4. 代码演示

    1
    2
    3
    transform: translate3d(100px, 100px, 100px)
    /* 注意:x, y, z 对应的值不能省略,不需要填写用 0 进行填充 */
    transform: translate3d(100px, 100px, 0)

透视 perspective

  1. 知识点讲解

    • 如果想要网页产生 3D 效果需要透视(理解成 3D 物体投影的 2D 平面上)
    • 实际上模仿人类的视觉位置,可视为安排一直眼睛去看
    • 透视也称为视距,所谓的视距就是人的眼睛到屏幕的距离
    • 距离视觉点越近的在电脑平面成像越大,越远成像越小
    • 透视的单位是像素
  2. 知识要点

    • 透视需要写在被视察元素的父盒子上面

    • 注意下方图片

      • d:就是视距,视距就是指人的眼睛到屏幕的距离

      • z:就是 z 轴,z 轴越大(正值),我们看到的物体就越大

        class="lazyload" data-src="https://gitee.com/haoyongliang/resources/raw/master/images/%E5%89%8D%E7%AB%AF/css/css3%E9%AB%98%E7%BA%A7/perspective.png"

  3. 代码演示

    1
    2
    3
    body {
    perspective: 1000px;
    }

translateZ

  1. translateZperspecitve 的区别
    • perspecitve 给父级进行设置,translateZ 给 子元素进行设置不同的大小

3D 旋转rotateX

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

  1. 语法

    • transform: rotateX(45deg) – 沿着 x 轴正方向旋转 45 度
    • transform: rotateY(45deg) – 沿着 y 轴正方向旋转 45 度
    • transform: rotateZ(45deg) – 沿着 z 轴正方向旋转 45 度
    • transform: rotate3d(x, y, z, 45deg) – 沿着自定义轴旋转 45 deg 为角度
  2. 代码案例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    div {
    perspective: 300px;
    }

    img {
    display: block;
    margin: 100px auto;
    transition: all 1s;
    }

    img:hover {
    transform: rotateX(-45deg)
    }
  1. 左手准则

    • 左手的手拇指指向 x 轴的正方向

    • 其余手指的弯曲方向就是该元素沿着 x 轴旋转的方向

      class="lazyload" data-src="https://gitee.com/haoyongliang/resources/raw/master/images/%E5%89%8D%E7%AB%AF/css/css3%E9%AB%98%E7%BA%A7/rotateX.png"

3D 旋转 rotateY

  1. 代码演示

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    div {
    perspective: 500px;
    }

    img {
    display: block;
    margin: 100px auto;
    transition: all 1s;
    }

    img:hover {
    transform: rotateY(180deg)
    }
  2. 左手准则

    • 左手的拇指指向 y 轴的正方向

    • 其余的手指弯曲方向就是该元素沿着 y 轴旋转的方向(正值)

      class="lazyload" data-src="https://gitee.com/haoyongliang/resources/raw/master/images/%E5%89%8D%E7%AB%AF/css/css3%E9%AB%98%E7%BA%A7/rotateY.png"

3D 旋转 rotateZ

  1. 代码演示

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    div {
    perspective: 500px;
    }

    img {
    display: block;
    margin: 100px auto;
    transition: all 1s;
    }

    img:hover {
    transform: rotateZ(180deg)
    }
  2. rotate3d

    • transform: rotate3d(x, y, z, deg) – 沿着自定义轴旋转 deg 为角度
    • x, y, z 表示旋转轴的矢量,是标识你是否希望沿着该轴进行旋转,最后一个标识旋转的角度
      • transform: rotate3d(1, 1, 0, 180deg) – 沿着对角线旋转 45deg
      • transform: rotate3d(1, 0, 0, 180deg) – 沿着 x 轴旋转 45deg
  3. 代码演示

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    div {
    perspective: 500px;
    }

    img {
    display: block;
    margin: 100px auto;
    transition: all 1s;
    }

    img:hover {
    transform: rotate3d(1, 1, 0, 180deg)
    }
    八、3D 呈现 transform-style
    1. transform-style

      • ☆☆☆☆☆

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

      • transform-style: flat 代表子元素不开启 3D 立体空间,默认的

      • transform-style: preserve-3d 子元素开启立体空间

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

    2. 代码演示

文章作者: 微信:hao_yongliang
文章链接: https://haoyongliang.gitee.io/2021/06/11/%E5%89%8D%E7%AB%AF/css/day09/HTML5CSS3_day02/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 郝永亮的主页
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论