一、CSS3
属性选择器
-
什么是
CSS3
- 在
CSS2
的基础上拓展、新增的样式
- 在
-
CSS3
发展现状- 移动端支持优于
PC
端 -
CSS3
目前还草案,在不断改进中 -
CSS3
相对H5
,应用非常广泛
- 移动端支持优于
-
属性选择器列表
-
attrcanshu.png
-
属性选择器代码演示
button { cursor: pointer; } button[disabled] { cursor: default }
-
代码演示
/*中间没有空格*/
input[type=search] {
color: skyblue;
}
span[class^=black] {
color: lightgreen;
}
span[class$=black] {
color: lightsalmon;
}
span[class*=black] {
color: lightseagreen;
}
二、结构伪类选择器
- 属性列表
-
jiegouweilei.png
-
代码演示
ul li:first-child { background-color: lightseagreen; } ul li:last-child { background-color: lightcoral; } ul li:nth-child(3) { background-color: aqua; }
(一)nth-child参数详解
-
nth-child 详解
-
注意:本质上就是选中第几个子元素
-
n 可以是数字、关键字、公式
-
n 如果是数字,就是选中第几个
-
常见的关键字有
even
偶数、odd
奇数 -
常见的公式如下(如果 n 是公式,则从 0 开始计算)
-
但是第 0 个元素或者超出了元素的个数会被忽略
-
-
nthchildcanshu.png
-
代码演示
<style> /* 偶数 */ ul li:nth-child(even) { background-color: aquamarine; } /* 奇数 */ ul li:nth-child(odd) { background-color: blueviolet; } /*n 是公式,从 0 开始计算 */ ul li:nth-child(n) { background-color: lightcoral; } /* 偶数 */ ul li:nth-child(2n) { background-color: lightskyblue; } /* 奇数 */ ul li:nth-child(2n + 1) { background-color: lightsalmon; } /* 选择第 0 5 10 15, 应该怎么选 */ ul li:nth-child(5n) { background-color: orangered; } /* n + 5 就是从第5个开始往后选择 */ ul li:nth-child(n + 5) { background-color: peru; } /* -n + 5 前五个 */ ul li:nth-child(-n + 5) { background-color: tan; } </style>
(二)nth-child和 nt-of-type 的区别
-
代码演示
<style> div :nth-child(1) { background-color: lightblue; } /*div的孩子中的第2个 且该孩子是span类型*/ div span:nth-child(2) { background-color: lightpink; } /*div的span孩子中的第2个*/ div span:nth-of-type(2) { background-color: lightseagreen; } div span:nth-of-type(3) { background-color: #fff; } </style>
-
区别
-
nth-child
选择父元素里面的第几个子元素,不管是第几个类型 -
nt-of-type
选择指定类型的元素
-
三、伪元素选择器
- 伪元素选择器
-
weiyuansu.png
👌在盒子内容 的前/后 插入行内元素
-
注意事项
-
before
和after
必须有content
属性 -
before
在内容前面,after 在内容后面 -
before
和after
创建的是一个元素,但是属于行内元素 - 创建出来的元素在
Dom
中查找不到,所以称为伪元素 - 伪元素和标签选择器一样,权重为 1
-
-
代码演示
<style> div { width: 100px; height: 100px; border: 1px solid lightcoral; } div::after, div::before { display: inline-block; width: 20px; height: 50px; text-align: center; } div::after { content: '德'; background-color: lightskyblue; } div::before { content: '道'; background-color: mediumaquamarine; } </style>
- 案例
-
添加字体图标
p { width: 220px; height: 22px; border: 1px solid lightseagreen; margin: 60px; position: relative; } p::after { content: '\ea50'; font-family: 'icomoon'; position: absolute; top: -1px; right: 10px; }
四、2D
转换
(一)translate移动
使得盒子移动的方法:定位、外边距、2d转换移动
-
2D
转换-
2D
转换是改变标签在二维平面上的位置和形状 -
移动:
translate
-
旋转:
rotate
-
缩放:
scale
-
-
translate
语法- x 就是 x 轴上水平移动
- y 就是 y 轴上水平移动
transform: translate(x, y) transform: translateX(n) transfrom: translateY(n)
-
重点知识点
-
2D
的移动主要是指 水平、垂直方向上的移动 -
translate
最大的优点就是不影响其他元素的位置 -
translate
中的100%单位,是相对于本身的宽度和高度来进行计算的 - 行内标签没有效果
-
-
代码演示
div {
background-color: lightseagreen;
width: 200px;
height: 100px;
/* 平移 */
/* 水平垂直移动 100px */
/* transform: translate(100px, 100px); */
/* 水平移动 100px */
/* transform: translate(100px, 0) */
/* 垂直移动 100px */
/* transform: translate(0, 100px) */
/* 水平移动 100px */
/* transform: translateX(100px); */
/* 垂直移动 100px */
transform: translateY(100px)
/* 如果单位不是px,是% 移动的距离是相对盒子自身的宽度/高度对比的 */
transform: translate(-50%, -50%)
/*盒子往上走和往左走自己高度和宽度的一半*/
}
(二)rotate旋转
-
rotate 旋转
-
2D
旋转指的是让元素在二维平面内顺时针或者逆时针旋转
-
-
rotate
语法/* 单位是:deg */ transform: rotate(度数)
-
重点知识点
-
rotate
里面跟度数,单位是 deg - 角度为正时,顺时针,角度为负时,逆时针
- 默认旋转的中心点是元素的中心点
-
-
代码演示
img:hover { transform: rotate(360deg) } img { transition: all 0.3s; /* 过渡加到本身上,谁做动画给谁加 */ }
1)三角案例
div {
position: relative;
width: 240px;
height: 35px;
border: 1px solid #000;
}
div::after {
content: "";
position: absolute;
top: 0;
right: 0;
width: 10px;
height: 10px;
border-right: 1px solid #000;
border-bottom: 1px solid #000;
transform: rotate(45deg);
transition: all 0.2s;/*谁做动画hover给谁加*/
}
/*鼠标经过div 里面的三角旋转 */
div:hover::after {
transform: rotate(225deg);
}
2)设置旋转中心点(transform-origin)
-
transform-origin
基础语法transform-origin: x y;
-
重要知识点
- 注意后面的参数 x 和 y 用空格隔开
- x y 默认旋转的中心点是元素的中心 (50% 50%),等价于
center
center
- 还可以给 x y 设置像素或者方位名词(
top
、bottom
、left
、right
、center
)
案例
div {
width: 200px;
height: 200px;
background-color: pink;
margin: 100px auto;
transition: all 1s;
/* 1.可以跟方位名词 */
/* transform-origin: left bottom; */
/* 2. 默认的是 50% 50% 等价于 center center */
/* 3. 可以是px 像素 */
transform-origin: 50px 50px;
}
div:hover {
transform: rotate(360deg);
}
3)旋转中心案例
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 里面的before 复原 */
div:hover::before {
transform: rotate(0deg);
}
(三)scale缩放
-
scale
的作用- 用来控制元素的放大与缩小
-
语法
transform: scale(x, y)
-
知识要点
- 注意,x 与 y 之间使用逗号进行分隔
-
transform: scale(1, 1)
: 宽高都放大一倍,相当于没有放大 -
transform: scale(2, 2)
: 宽和高都放大了二倍 -
transform: scale(2)
: 只写了一个参数,等比例缩放(x=y=2) -
transform:scale(0.5, 0.5)
: 缩小 -
scale
最大的优势:可以设置缩放中心点,默认以中心点缩放;而且不影响其他盒子(width height缩放以上边框缩放 影响后面盒子)
-
代码演示
div:hover { /* 注意,数字是倍数的含义,所以不需要加单位 */ /* transform: scale(2, 2) */ /* 实现等比缩放,同时修改宽与高 */ /* transform: scale(2) */ /* 小于 1 就等于缩放*/ transform: scale(0.5, 0.5) } div { /* 设置缩放中心点 */ transform-origin: left bottom; }
(1)图片放大案例
div {
overflow: hidden;
float: left;
margin: 10px;
}
div img {
transition: all .4s;
}
div img:hover {
transform: scale(1.1);
}
(2)分页按钮案例
li {
float: left;
width: 30px;
height: 30px;
border: 1px solid pink;
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);
}
(3)2d转换综合案例
div {
width: 200px;
height: 200px;
background-color: pink;
transition: all .5s;
}
div:hover {
/* transform: rotate(180deg) translate(150px, 50px); */
/* 我们同时有位移和其他属性,我们需要把位移放到最前面 */
transform: translate(150px, 50px) rotate(180deg) scale(1.2);
}
五、动画
(一)基本使用
1、动画的基本使用
- 先定义动画
- 在调用定义好的动画
2、语法格式(定义动画)
@keyframes 动画名称 {
0% {
width: 100px;
}
100% {
width: 200px
}
}
语法格式(使用动画)
div {
/* 调用动画 */
animation-name: 动画名称;
/* 持续时间 */
animation-duration: 持续时间;
}
3、动画序列
- 0% 是动画的开始,100 % 是动画的完成,这样的规则就是动画序列
- 在 @keyframs 中规定某项 CSS 样式,就由创建当前样式逐渐改为新样式的动画效果
- 动画是使元素从一个样式逐渐变化为另一个样式的效果,可以改变任意多的样式任意多的次数
- 用百分比来规定变化发生的时间,或用
from
和to
,等同于 0% 和 100%
4、演示
<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>
案例2
div {
width: 100px;
height: 100px;
background-color: #666;
animation-name: move;
animation-duration: 5s;
}
/* multiple state : */
@keyframes move{
/* int and time */
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);
}
}
(二)常见属性
1、animation-duration(持续时间)
2、animation-name(动画名)
3、animation-timing-function(时间曲线)
ease:(默认)值,动画先慢后快,在即将结束时再变慢
ease-out:动画以高速开始,以低速结束
ease-in:动画以低速开始,以高速结束
linear:动画从头到尾的速度是相同的
ease-in-out:动画由慢到快再到慢直至结束
steps():指定时间函数中的间隔数量(步长)
ease不同的是它均等得分为三份,而ease是只在结束时变慢;
贝赛尔函数:规定动画的播放时间曲线
4、animation-delay(动画延迟时间)
默认是0
5、animation-iteration-count(动画播放次数)
默认是1,即一次。值infinite是无限循环
6、animation-direction(动画反向播放)
normal:(默认)值,不进行反方向播放;
reverse:全部播放都是用反方向播放;
alternate:在奇数次数时正向播放,偶数反向播放;
alternate-reverse:在偶数次数正向播放,奇数反向播放;
7、animation-fill-mode(动画保持开始或者结束时的样式)
forwards:动画结束后保持结束时的样式;
backwards:动画结束后 回到开始时的状态
8、animation-play-state(暂停动画)
running:动画运行;(默认)
paused:动画暂停;
代码演示
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;
}
(三)动画简写方式
animation: name duration timing-function delay iteration-count direction fill-mode play-state;
前2个属性一定要写
知识要点
- 简写属性里面不包含
animation-paly-state
- 暂停动画
animation-paly-state: paused
; 经常和鼠标经过等其他配合使用 - 要想动画走回来,而不是直接调回来:
animation-direction: alternate
- 盒子动画结束后,停在结束位置:
animation-fill-mode: forwards
演示
animation: move 2s linear 1s infinite alternate forwards;
(四)热点案例
<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 taibei">
<div class="dotted"></div>
<div class="pulse1"></div>
<div class="pulse2"></div>
<div class="pulse3"></div>
</div>
</div>
</body>
body {
background-color: black;
}
.map {
position: relative;
width: 747px;
height: 616px;
background: url(media/map.png) no-repeat;
margin: 0 auto;
}
.city {
position: absolute;
top: 227px;
right: 193px;
}
.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 #009df9;
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% {
/* transform: scale(5); */
width: 40px;
height: 40px;
opacity: 1;
}
100% {
/* transform: scale(7); */
width: 70px;
height: 70px;
opacity: 0;
}
}
.taibei {
top: 500px;
right: 80px;
}
(五)animation-timing-function:steps()
div {
overflow: hidden;
font-size: 20px;
width: 0;
height: 30px;
background-color: pink;
/* 让我们的文字强制一行内显示 */
white-space: nowrap;
/* steps 就是分几步来完成我们的动画 有了steps 就不要在写 ease 或者linear 了 */
animation: w 4s steps(10) forwards;
}
@keyframes w {
0% {
width: 0;
}
100% {
width: 200px;
}
}
<div>世纪佳缘我在这里等你</div>
案例:奔跑的小熊(百度浏览器)
- 代码演示
<!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>
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%;
/* margin-left: -100px; */
transform: translateX(-50%);
}
}
</style>
</head>
<body>
<div></div>
</body>
</html>
六、3D转换
三维坐标系
- x 轴:水平向右 -- 注意:x 轴右边是正值,左边是负值
- y 轴:垂直向下 -- 注意:y 轴下面是正值,上面是负值
- z 轴:垂直屏幕 -- 注意:往外边(靠近眼睛)的是正值,往里面(远离眼睛)的是负值,单位一般是px
(一)3D移动
-
transform: translateX(100px)
:仅仅是在 x 轴上移动 -
transform: translateY(100px)
:仅仅是在 y 轴上移动 -
transform: translateZ(100px)
:仅仅是在 z 轴上移动 -
transform: translate3d(x, y, z)
:其中x、y、z 分别指要移动的轴的方向的距离 - 注意:x, y, z 对应的值不能省略,不用时要用 0 进行填充
-
语法
transform: translate3d(x, y, z)
-
get start
transform: translate3d(100px, 100px, 100px) /* 注意:x, y, z 对应的值不能省略,不需要填写用 0 进行填充 */ transform: translate3d(100px, 100px, 0)
(二)透视 perspective
- 透视也称为视距,所谓的视距就是人的眼睛到屏幕的距离
- 距离视觉点越近的在电脑平面成像越大,越远成像越小
- 透视的单位是像素
1、知识要点
-
透视需要写在被视察元素的父盒子上面
-
d:就是视距,视距就是指人的眼睛到屏幕的距离,d越小成像越大
-
z:就是 z 轴,z 轴越大(正值),我们看到的物体就越大
-
2、get start
body {
perspective: 1000px;
}
3、translateZ
与 perspecitve
的区别
perspecitve
给父级进行设置,translateZ
给 子元素进行设置不同的大小
(三)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、rotateX
(1)get start
div {
perspective: 300px;
}
img {
display: block;
margin: 100px auto;
transition: all 1s;
}
img:hover {
transform: rotateX(-45deg)
}
(2)左手准则
-
左手的手拇指指向 x 轴的正方向
-
其余手指的弯曲方向就是该元素沿着 x 轴旋转的方向
3、rotateY
(1)get start
div {
perspective: 500px;
}
img {
display: block;
margin: 100px auto;
transition: all 1s;
}
img:hover {
transform: rotateY(180deg)
}
(2)左手准则
-
左手的拇指指向 y 轴的正方向
-
其余的手指弯曲方向就是该元素沿着 y 轴旋转的方向(正值)
4、rotateZ
(1)get start
div {
perspective: 500px;
}
img {
display: block;
margin: 100px auto;
transition: all 1s;
}
img:hover {
transform: rotateZ(180deg)
}
5、rotate3d
(1)语法
-
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
-
(2)get start
div {
perspective: 500px;
}
img {
display: block;
margin: 100px auto;
transition: all 1s;
}
img:hover {
transform: rotate3d(1, 1, 0, 180deg)
}
(四)3D
呈现 transform-style
-
transform-style
-
控制子元素是否开启三维立体环境
transform-style: flat
代表子元素不开启3D
立体空间,默认的transform-style: preserve-3d
子元素开启立体空间 -
代码写给父级,但是影响的是子盒子
-
-
get start
body {
perspective: 500px;
}
.box {
position: relative;
width: 200px;
height: 200px;
margin: 100px auto;
transition: all 2s;
/* 让子元素保持3d立体空间环境 */
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);
}
<body>
<div class="box">
<div></div>
<div></div>
</div>
</body>
七、浏览器私有前缀
-
为了让CSS3样式兼容,需要将某些样式加上浏览器前缀:
-ms- 兼容IE浏览器
-moz- 兼容firefox
-o- 兼容opera
-webkit- 兼容chrome 和 safari -
get start
div
{
-ms-transform: rotate(30deg);
-webkit-transform: rotate(30deg);
-o-transform: rotate(30deg);
-moz-transform: rotate(30deg);
transform: rotate(30deg);
}
网友评论