学习前端CSS【前端学习3】HTML5和CSS3提高
理理本文是前端之CSS进阶知识点总结,因为前端学习2写的太长了、也算比较全面,于是再写一个进阶篇。
HTML5新特性
HTML5语义化标签
只使用IE9以上的。IE9需要转化为block
<header>
头部标签;
<nav
导航标签;
<article>
内容标签;
<section>
定义文档某个区域;
<aside>
侧边栏目标签;
<footer>
尾部标签;
视频标签
1
| <video src = "" controls = "controls" autoplay = "autoplay" muted = "muted" loop = "loop"></video>
|
音频标签
1
| <audio src="" controls="controls" loop="loop" autoplay="autoplay" ></audio>
|
Input表单
其实我在前端HTML部分写过。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <body> //必须添加form表单域 <form action=""> <ul> <li>邮箱:<input type="email" /></li> <li>网址:<input type="url" /></li> <li>日期:<input type="date" /></li> <li>日期:<input type="time" /></li> <li>数量:<input type="number" /></li> <li>手机号码:<input type="tel" /></li> <li>搜索:<input type="search" /></li> <li>颜色:<input type="color" /></li> <li><input type="submit" value="提交" /></li> </ul> </form> </body>
|
表单属性
其实我在前端HTML部分写过。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 1. **required**: - 属性值:`required` - 说明:表单拥有该属性表示其内容不能为空,是必填项。
2. **placeholder**: - 属性值:`placeholder` - 说明:表单的提示信息,用于在输入框中显示提示文本,如果输入框中存在默认值,则该提示文本将不会显示。
3. **autofocus**: - 属性值:`autofocus` - 说明:自动聚焦属性,当页面加载完成后,会自动将焦点聚焦到指定的表单元素上。
4. **autocomplete**: - 属性值:`off` / `on` - 说明:当用户在字段开始键入时,浏览器会基于之前键入过的值,自动显示在字段中可能填写的选项。默认情况下,autocomplete功能是打开的(`on`),如果需要关闭该功能,可以设置为`off`。该属性需要放在表单内,并且通常需要配合`name`属性一起使用,以确保表单能够成功提交。
5. **multiple**: - 属性值:`multiple` - 说明:该属性用于文件上传输入框,允许用户选择多个文件进行提交。
|
CSS3新特性
属性选择器
我在前端学习2之前写过。类选择器】属性选择器、伪类选择器权重都是10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| input[value] { //input标签且含有value属性 } input[type="value"] { //input有type且值为value } input[type^="va"] { 开头为va的 } input[type$="va"] { 结尾为va的 } input[type*="va"] { 含有va的 }
|
结构伪类选择器
1 2 3 4 5 6
| 比如选择ul里的第一个小li是 ul li:first-child; 最后一个是 ul li:last-child; 最重要的是nth-child,选择第几个孩子 ul li:nth-child(n);
|
n可以是数字、关键字(even偶数,odd是奇数)做隔行变色,还能是公式(这里直接填n默认所有,只能写n不能其他字母)
1 2 3 4 5 6 7
| 关于公式—— n:全部; 2n:所有偶数; 2n+1:所有奇数; 5n:5的倍数; n+5:从第五个开始; -n+5:从第五个开始倒着来;
|
接下来是nth-of-type,和上面类似
nth-child(n)是会选中所有的孩子、什么div什么p标签都接纳。
nth-of-type(n)只会选择该类中的,比如我们指定好div了就只管div,我喜欢这种。
伪元素选择器
利用CSS创建新标签元素而不用HTML创建标签。
因为他不是html元素,在文档树找不到,所以是伪元素
1 2 3
| ::before在元素内部前面插入内容 ::after在元素内部后面插入内容 都是孩子元素
|
伪元素选择器使用方法:element(元素标签)::before/after{},其中必须有content属性
使用场景:
1.做字体图标,我们不用div写盒子了。
2.仿土豆网、鼠标放视频上变黑的效果。
3.清除浮动:额外标签法变形,关门教训。
CSS3盒子模型
box-sizing: content-box;——我们以前的盒子模型
box-sizing: border-box;——无论边框、内边距如何已经定死的盒子
CSS3图片模糊和盒子宽度计算
模糊:filter:函数();如fliter: blur(5px);数值越大越模糊。
宽度计算:width: calc(100% - 80px);
CSS3过渡
过渡是简单动画,经常和hover一起用。
transition: 要过度的属性 花费时间(必须写单位s) 运动曲线(默认ease可以省略) 何时开始(默认0s,必须写单位)
ease:逐渐慢;linear:匀速;ease-in:加速;ease-out:减速;ease-in-out:先加速再减速;
谁做过度给谁加、可以加逗号分隔多个属性,也可以用all选中所有属性(最常用)
1 2 3
| transition: width .5s ease 0s,height .5s ease 0s; transition: all .5s;
|
2D转换
1.translate
1 2 3 4
| transform: translate(x, y); /* 或者分开写 */ transform: translateX(n); transform: translateY(n);
|
重点:
- 定义 2D 转换中的移动,沿着 X 和 Y 轴移动元素
- translate 最大的优点:不会影响到任何其他元素的位置(优于定位的地方)
- translate 中的百分比单位是相对于自身元素的 translate: (50%, 50%);
- 对行内元素没有效果
2D 转换之旋转 rotate
2D 旋转指的是让元素在 2 维平面内顺时针旋转或者逆时针旋转。
语法:
重点:
- rotate 里面跟度数,单位是 deg,比如 rotate(45deg)
- 角度为正时,顺时针;负时,逆时针
- 默认旋转的中心点是元素的中心点
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
| <!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>2D转换之旋转rotate</title> <style> img { width: 150px; border-radius: 50%; border: 5px solid pink; transition: all 0.5s; }
img:hover { transform: rotate(360deg); } </style> </head>
<body> <img src="media/pic.jpg" alt=""> </body>
</html>
|
我们可以设置元素转换的中心点。
语法:
重点:
- 注意后面的参数 x 和 y 用空格隔开
- x y 默认转换的中心点是元素的中心点(50% 50%)
- 还可以给 x y 设置 像素 或者 方位名词(top bottom left right center)
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
| <!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>transform-origin</title> <style> div { width: 100px; height: 100px; background-color: pink; margin: 100px auto; transition: all 1s; transform-origin: 25px 25px; }
div:hover { transform: rotate(360deg); } </style> </head>
<body> <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 39 40 41 42 43 44
| <!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>旋转中心点</title> <style> div { overflow: hidden; width: 200px; height: 200px; border: 1px solid pink; margin: 10px; float: left; }
div::before { content: "黑马"; display: block; width: 100%; height: 100%; background-color: hotpink; transform: rotate(180deg); transform-origin: left bottom; transition: all 0.4s; }
div:hover::before { transform: rotate(0deg); } </style> </head>
<body> <div></div> <div></div> <div></div> </body>
</html>
|
1.5 2D 转换之缩放 scale
缩放,顾名思义,可以放大和缩小。只要给元素添加上了这个属性就能控制它放大还是缩小。
语法:
注意:
- 注意其中的 x 和 y 用逗号分隔
- transform: scale(1, 1) :宽和高都放大一倍,相当于没有放大
- transform: scale(2, 2) :宽和高都放大了 2 倍
- transform: scale(2) :只写一个参数,第二个参数默认等于第一个参数,相当于 scale(2, 2)
- transform: scale(0.5, 0.5) :缩小
- scale 缩放最大的优势:可以设置缩放的基准点(默认以中心点缩放);并且缩放不会影响其他盒子的位置(以上两个特点都是直接设置 width 和 height 都无法做到的)
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
| <!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>2D转换之缩放</title> <style> div { width: 200px; height: 200px; background-color: pink; margin: 100px auto; } div:hover {
transform: scale(2); } </style> </head>
<body> <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 39 40
| <!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>图片放大案例</title> <style> div { width: 225px; height: 137px; overflow: hidden; float: left; margin: 10px; }
div img { transition: all .4s; }
div img:hover { transform: scale(1.1); } </style> </head>
<body> <div> <a href="#"><img src="media/scale.jpg" alt=""></a> </div> <div> <a href="#"><img src="media/scale.jpg" alt=""></a> </div> <div> <a href="#"><img src="media/scale.jpg" alt=""></a> </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
| <!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>Document</title> <style> li { float: left; width: 30px; height: 30px; border: 1px solid hotpink; margin: 10px; text-align: center; line-height: 30px; list-style: none; border-radius: 50%; cursor: pointer; transition: all .4s; }
li:hover { transform: scale(1.2); } </style> </head>
<body> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> </ul> </body>
</html>
|

1.6 2D 转换综合写法
注意:
- 同时使用多个转换,其格式为:
transform: translate() rotate() scale()
…等
- 其顺序会影转换的效果。(先旋转会改变坐标轴方向)
- 当我们同时有位移和其他属性的时候,记得要将位移放到最前
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
| <!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>Document</title> <style> div { width: 200px; height: 200px; background-color: pink; transition: all 1s; }
div:hover { transform: translate(150px, 50px) rotate(180deg) scale(1.2); } </style> </head>
<body> <div></div> </body>
</html>
|
1.7 2D 转换总结
- 转换 transform 我们简单理解就是变形,有 2D 和 3D 之分
- 我们暂且学了三个,分别是:位移、旋转 和 缩放
- 2D 移动 translate(x, y) 最大的优势是不影响其他盒子,里面参数用 %,是相对于自身宽度和高度来计算的
- 可以分开写比如 translateX(x) 和 translateY(y)
- 2D 旋转 rotate(度数) 可以实现旋转元素,度数的单位是 deg
- 2D 缩放 sacle(x, y) 里面参数是数字,不跟单位,可以是小数。最大的优势在于不影响其他盒子
- 设置转换中心点 transform-origin : x y; 参数可以百分比、像素或者是方位名词
- 当我们进行综合写法,同时有位移和其他属性的时候,记得要将位移放到最前
二、CSS3 动画
动画(animation)是 CSS3 中具有颠覆性的特征之一,可通过设置多个节点来精确控制一个或一组动画,常用来实现复杂的动画效果。
相比较过渡,动画可以实现更多变化,更多控制,连续自动播放等效果。
2.1 动画的基本使用
制作动画分为两步:
- 先定义动画
- 再使用(调用)动画
2.1.1 用 keyframes 定义动画(类似定义类选择器)
1 2 3 4 5 6 7 8
| @keyframes 动画名称 { 0% { width: 100px; } 100% { width: 200px; } }
|
动画序列
- 0% 是动画的开始,100% 是动画的完成。这样的规则就是动画序列
- 在 @keyframes 中规定某项 CSS 样式,就能创建由当前样式逐渐改为新样式的动画效果
- 动画是使元素从一种样式逐渐变化为另一种样式的效果。您可以改变任意多的样式任意多的次数
- 请用百分比来规定变化发生的时间,或用关键词 “from” 和 “to”,等同于 0% 和 100%
2.1.2 元素使用动画
1 2 3 4 5 6 7 8 9 10
| div { width: 200px; height: 200px; background-color: aqua; margin: 100px auto; animation-name: 动画名称; animation-duration: 持续时间; }
|
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
| <!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>CSS3动画的基本使用</title> <style> @keyframes move { 0% { transform: translateX(0px); } 100% { transform: translateX(1000px); } }
div { width: 200px; height: 200px; background-color: pink; animation-name: move; animation-duration: 2s; } </style> </head>
<body> <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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| <!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>动画序列</title> <style>
@keyframes move { 0% { transform: translate(0, 0); } 25% { transform: translate(1000px, 0) } 50% { transform: translate(1000px, 500px); } 75% { transform: translate(0, 500px); } 100% { transform: translate(0, 0); } }
div { width: 100px; height: 100px; background-color: pink; animation-name: move; animation-duration: 10s; } </style> </head>
<body> <div>
</div> </body>
</html>
|

2.2 动画常用属性
属性 |
描述 |
@keyframes |
规定动画 |
animation |
所有动画属性的简写属性,除了animation-play-state 属性 |
animation-name |
规定 @keyframes 动画的名称(必须的) |
animation-duration |
规定动画完成一个周期所花费的秒或毫秒,默认是 0(必须的) |
animation-timing-function |
规定动画的速度曲线,默认是 “ease” |
animation-delay |
规定动画何时开始,默认是 0 |
animation-iteration-count |
规定动画被播放的次数,默认是 1,还有 infinite |
animation-direction |
规定动画是否在下一周期逆向播放,默认是 “normal”, alternate 逆播放 |
animation-play-state |
规定动画是否正在运行或暂停。默认是 “running”, 还有 “paused” |
animation-fill-mode |
规定动画结束后状态,保持 forwards 回到起始 backwards |
2.3 动画简写属性
animation:动画名称 持续时间 运动曲线 何时开始 播放次数 是否反方向 动画起始或者结束的状态。
1
| animation: myfirst 5s linear 2s infinite alternate;
|
- 简写属性里面不包含 animation-play-state
- 暂停动画:animation-play-state: puased; 经常和鼠标经过等其他配合使用
- 想要动画走回来,而不是直接跳回来:animation-direction: alternate
- 盒子动画结束后,停在结束位置:animation-fill-mode: forwards
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
| <!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>动画属性</title> <style> @keyframes move { 0% { transform: translate(0, 0); } 100% { transform: translate(1000px, 0); } }
div { width: 100px; height: 100px; background-color: pink; animation-name: move; animation-delay: 1s; }
div:hover { animation-play-state: paused; } </style> </head>
<body> <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 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| <!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>大数据热点图</title> <style> body { background-color: #333; }
.map { position: relative; width: 747px; height: 616px; background: url(media/map.png) no-repeat; margin: 0 auto; }
.city { position: absolute; top: 227px; right: 193px; color: #fff; }
.tb {
top: 500px; right: 80px; }
.dotted { width: 8px; height: 8px; background-color: #09f; border-radius: 50%; }
.city div[class^="pulse"] { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 8px; height: 8px; box-shadow: 0 0 12px #009dfd; border-radius: 50%; animation: pulse 1.2s linear infinite; }
.city div.pulse2 { animation-delay: 0.4s; }
.city div.pulse3 { animation-delay: 0.8s; }
@keyframes pulse { 0% { } 70% { width: 40px; height: 40px; opacity: 1; } 100% { width: 70px; height: 70px; opacity: 0; } } </style> </head>
<body> <div class="map"> <div class="city"> <div class="dotted"></div> <div class="pulse1"></div> <div class="pulse2"></div> <div class="pulse3"></div> </div> <div class="city tb"> <div class="dotted"></div> <div class="pulse1"></div> <div class="pulse2"></div> <div class="pulse3"></div> </div> </div> </body>
</html>
|

2.4 速度曲线细节
animation-timing-function:规定动画的速度曲线,默认是 “ease”。
值 |
描述 |
linear |
动画从头到尾的速度是相同的(匀速) |
ease |
默认。动画以低速开始,然后加快,在结束前变慢 |
ease-in |
动画以低速开始 |
ease-out |
动画以低速结束 |
ease-in-out |
动画以低速开始和结束 |
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 37
| <!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>速度曲线步长</title> <style> div { overflow: hidden; font-size: 20px; width: 0; height: 30px; background-color: pink; white-space: nowrap; animation: w 4s steps(10) forwards; }
@keyframes w { 0% { width: 0; } 100% { width: 200px; } } </style> </head>
<body> <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 39 40 41 42 43 44 45 46 47 48 49
| <!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>奔跑的熊大案例</title> <style> body { background-color: #ccc; }
div { position: absolute; width: 200px; height: 100px; background: url(media/bear.png) no-repeat; animation: bear .4s steps(8) infinite, move 3s forwards; }
@keyframes bear { 0% { background-position: 0 0; } 100% { background-position: -1600px 0; } }
@keyframes move { 0% { left: 0; } 100% { left: 50%; transform: translateX(-50%); } } </style> </head>
<body> <div></div> </body>
</html>
|


三、CSS3 3D转换
我们生活的环境是 3D 的,照片就是 3D 物体在 2D 平面呈现的例子。
有什么特点
当我们在网页上构建 3D 效果的时候参考这些特点就能产出 3D 效果。
3.1 三维坐标系
三维坐标系其实就是指立体空间,立体空间是由3个轴共同组成的。
- x 轴:水平向右(注意:x 右边是正值,左边是负值)
- y 轴:垂直向下(注意:y 下面是正值,上面是负值)
- z 轴:垂直屏幕(注意:往外面是正值,往里面是负值)
3D 转换我们主要学习工作中最常用的 3D 位移 和 3D 旋转。
主要知识点
- 3D 位移:translate3d(x, y, z)
- 3D 旋转:rotate3d(x, y, z)
- 透视:perspective
- 3D 呈现:transfrom-style
3.2 3D移动 translate3d
3D 移动在 2D 移动的基础上多加了一个可以移动的方向,就是 z 轴方向。
- transform:translateX(100px):仅仅是在 X 轴上移动
- transform:translateY(100px):仅仅是在 Y 轴上移动
- transform:translateZ(100px):仅仅是在 Z 轴上移动(注意:translateZ 一般用 px 单位)
- transform:translate3d(x, y, z):其中 x、y、z 分别指要移动的轴的方向的距离
因为 z 轴是垂直屏幕,由里指向外面,所以默认是看不到元素在 z 轴的方向上移动(要借助透视)。
3.3 透视 perspective
在 2D 平面产生近大远小视觉立体,但是效果只是二维的。
- 如果想要在网页产生 3D 效果需要透视(理解成 3D 物体投影在 2D 平面内)
- 模拟人类的视觉位置,可认为安排一只眼睛去看
- 透视我们也称为视距:视距就是人的眼睛到屏幕的距离
- 距离视觉点越近的,在电脑平面成像越大,越远成像越小
- 透视的单位是像素
透视写在被观察元素的父盒子上面。
d:就是视距,视距就是一个距离人的眼睛到屏幕的距离。
z:就是 z 轴,物体距离屏幕的距离,z 轴越大(正值)我们看到的物体就越大。
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 name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>3D移动translate3d</title> <style> body { perspective: 200px; }
div { width: 200px; height: 200px; background-color: pink; transform: translate3d(400px, 100px, 100px); } </style> </head>
<body> <div></div> </body>
</html>
|

3.4 translateZ
translform:translateZ(100px):仅仅是在 Z 轴上移动。有了透视,就能看到 translateZ 引起的变化了。
- translateZ:近大远小
- translateZ:往外是正值
- translateZ:往里是负值
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
| <!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>translateZ</title> <style> body { perspective: 500px; }
div { width: 200px; height: 200px; background-color: pink; margin: 100px auto; transform: translateZ(0); } </style> </head>
<body> <div></div> <div></div> <div></div> </body>
</html>
|
3.5 3D旋转 rotate3d
3D旋转指可以让元素在三维平面内沿着 x轴,y轴,z轴或者自定义轴进行旋转。
语法
- transform: rotateX(45deg):沿着 x 轴正方向旋转 45 度
- transform: rotateY(45deg):沿着 y 轴正方向旋转 45deg
- transform: rotateZ(45deg):沿着 z 轴正方向旋转 45deg
- transform: rotate3d(x, y, z, deg):沿着自定义轴旋转 deg 为角度(了解即可)

对于元素旋转的方向的判断,我们需要先学习一个左手准则。
左手准则
- 左手的手拇指指向 x 轴的正方向
- 其余手指的弯曲方向就是该元素沿着 x 轴旋转的方向
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
| <!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>rotateX</title> <style> body { perspective: 300px; }
img { display: block; margin: 100px auto; transition: all 1s; }
img:hover { transform: rotateX(45deg); } </style> </head>
<body> <img src="media/pig.jpg" alt=""> </body>
</html>
|
- 左手的手拇指指向 y 轴的正方向
- 其余手指的弯曲方向就是该元素沿着 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
| <!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>rotateY</title> <style> body { perspective: 500px; }
img { display: block; margin: 100px auto; transition: all 1s; }
img:hover { transform: rotateY(45deg); } </style> </head>
<body> <img src="media/pig.jpg" alt=""> </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
| <!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>rotateZ</title> <style> body { perspective: 500px; }
img { display: block; margin: 100px auto; transition: all 1s; }
img:hover { transform: rotateZ(180deg); } </style> </head>
<body> <img src="media/pig.jpg" alt=""> </body>
</html>
|
transform: rotate3d(x, y, z, deg):沿着自定义轴旋转 deg 为角度(了解即可)。
xyz 是表示旋转轴的矢量,表示你是否希望沿着该轴旋转,最后一个表示旋转的角度。
- transform: rotate3d(1, 0, 0, 45deg):就是沿着 x 轴旋转 45deg
- transform: rotate3d(0, 1, 0, 45deg):就是沿着 y 轴旋转 45deg
- transform: rotate3d(0, 0, 1, 45deg):就是沿着 z 轴旋转 45deg
- transform: rotate3d(1, 1, 0, 45deg):就是沿着对角线(矢量计算)旋转 45deg
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
| <!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>rotate3d</title> <style> body { perspective: 500px; }
img { display: block; margin: 100px auto; transition: all 1s; }
img:hover { transform: rotate3d(1, 1, 0, 45deg); } </style> </head>
<body> <img src="media/pig.jpg" alt=""> </body>
</html>
|
3.6 3D呈现 transfrom-style
- 控制子元素是否开启三维立体环境
- transform-style: flat 子元素不开启 3d 立体空间(默认的)
- transform-style: preserve-3d; 子元素开启立体空间
- 代码写给父级,但是影响的是子盒子
- 这个属性很重要,后面必用

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
| <!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>transform-style</title> <style> body { perspective: 500px; }
.box { position: relative; width: 200px; height: 200px; margin: 100px auto; transition: all 2s; transform-style: preserve-3d; }
.box:hover { transform: rotateY(60deg); }
.box div { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: pink; }
.box div:last-child { background-color: purple; transform: rotateX(60deg); } </style> </head>
<body> <div class="box"> <div></div> <div></div> </div> </body>
</html>
|
【案例:两面翻转的盒子】
实现步骤:
- 搭建 HTML 结构
1 2 3 4
| <div class="box"> <div class="front">黑马程序员</div> <div class="back">pink老师等你</div> </div>
|
- box 父盒子里面包含前后两个子盒子
- box 是翻转的盒子 front 是前面盒子 back 是后面盒子
- CSS 样式
- box 指定大小,切记要添加 3d 呈现
- back 盒子要沿着 Y 轴翻转 180 度
- 最后鼠标经过 box 沿着 Y 旋转 180deg
代码:
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
| <!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>两面翻转的盒子</title> <style> body { perspective: 400px; }
.box { position: relative; width: 300px; height: 300px; margin: 100px auto; transition: all .4s; transform-style: preserve-3d; }
.box:hover { transform: rotateY(180deg); }
.front, .back { position: absolute; top: 0; left: 0; width: 100%; height: 100%; border-radius: 50%; font-size: 30px; color: #fff; text-align: center; line-height: 300px; }
.front { background-color: pink; z-index: 1; }
.back { background-color: purple; transform: rotateY(180deg); } </style> </head>
<body> <div class="box"> <div class="front">黑马程序员</div> <div class="back">pink老师这里等你</div> </div> </body>
</html>
|
【案例:3D 导航栏】

实现步骤:
- 搭建 HTML 结构
1 2 3 4 5 6 7 8
| <ul> <li> <div class="box"> <div class="front">黑马程序员</div> <div class="bottom">pink老师等你</div> </div> </li> </ul>
|
- li 做导航栏
- .box 是翻转的盒子 front 是前面盒子 bottom 是底下盒子
- CSS 样式
- li 设置大小,加透视和 3d 呈现
- front 需要前移 17.5 像素
- bottom 需要下移 17.5 像素并且要沿着 x 轴翻转 负 90 度
- 鼠标放到 box 让盒子旋转 90 度
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| <!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导航栏案例</title> <style> * { margin: 0; padding: 0; }
ul { margin: 100px; }
ul li { float: left; margin: 0 5px; width: 120px; height: 35px; list-style: none; perspective: 500px; }
.box { position: relative; width: 100%; height: 100%; transform-style: preserve-3d; transition: all .4s; }
.box:hover { transform: rotateX(90deg); }
.front, .bottom { position: absolute; left: 0; top: 0; width: 100%; height: 100%; }
.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">pink老师等你</div> </div> </li> <li> <div class="box"> <div class="front">黑马程序员</div> <div class="bottom">pink老师等你</div> </div> </li> <li> <div class="box"> <div class="front">黑马程序员</div> <div class="bottom">pink老师等你</div> </div> </li> <li> <div class="box"> <div class="front">黑马程序员</div> <div class="bottom">pink老师等你</div> </div> </li> <li> <div class="box"> <div class="front">黑马程序员</div> <div class="bottom">pink老师等你</div> </div> </li> <li> <div class="box"> <div class="front">黑马程序员</div> <div class="bottom">pink老师等你</div> </div> </li> </ul> </body>
</html>
|
【综合案例:旋转木马】

- 搭建 HTML 结构
1 2 3 4 5 6 7 8
| <section> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </section>
|
- 里面的 6 个 div 分别是 6 个狗狗图片
- 注意最终旋转是 section 标签旋转
- CSS 样式
- 给 body 添加 透视效果 perspective: 1000px;
- 给 section 添加大小,一定不要忘记添加 3d 呈现效果控制里面的 6 个 div
- 里面 6 个 div 全部绝对定位叠到一起,然后移动不同角度旋转和距离
- 注意:旋转角度用 rotateY 距离肯定用 translateZ 来控制
- 给 section 添加动画 animation,让它可以自动旋转即可
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| <!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>综合案例:旋转木马</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(media/pig.jpg) no-repeat; }
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(media/dog.jpg) no-repeat; }
section div:nth-child(1) { transform: rotateY(0) 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></div> <div></div> <div></div> <div></div> <div></div> <div></div> </section> </body>
</html>
|
四、浏览器私有前缀
浏览器私有前缀是为了兼容老版本的写法,比较新版本的浏览器无须添加。
4.1 私有前缀
- -moz-:代表 firefox 浏览器私有属性
- -ms-:代表 ie 浏览器私有属性
- -webkit-:代表 safari、chrome 私有属性
- -o-:代表 Opera 私有属性
4.2 提倡的写法
1 2 3 4
| -moz-border-radius: 10px; -webkit-border-radius: 10px; -o-border-radius: 10px; border-radius: 10px;
|