一、2d转化
变形属性:transform
transform翻译成汉语具有"变换"或者"改变"的意思。
通过此属性具有非常强大的功能,比如可以实现元素的位移、拉伸或者旋转等效果
最能体现transform 属性强大实力的是实现元素的3D变换效果
2D
2D变换,是在一个平面对元素进行的操作。
可以对元素进行水平或者垂直位移、旋转或者拉伸
二、变形属性:transform的所有属性值
1、transform:none;默认值
2、transform:translate();移动 平移 单位是px
3、transform:rotate();旋转 单位是deg deg度数
4、transform:scale();缩放 没有单位 默认值是1
5、transform:skew();倾斜 单位是deg
6、transform:matrix();矩阵
7、transform:perspective();景深 视距 单位是px
三、确定坐标系
对上面坐标系简单分析如下:
(1).默认状态下,x轴是水平的,向右为正。
(2).默认状态下,y轴是垂直的,向下为正,这与传统的数学坐标系不同。
四、translate平移
1、transform :none;定义不进行转换。
transform :translate(200px);平移,默认是X轴移动,可以单位是%,这个%是自己的,不是父元素的
2、transform :translate(200px);默认是按照X轴移动
3、transform :translateX(200px);根据X轴给定的参数,从当前元素位置移动。
4、transform :translateY(200px);根据Y轴给定的参数,从当前元素位置移动。
5、transform :translate(10px,20px);定义 2D 平移移动。
6、transform :translateX(200px) translateY(200px) ;
五、rotate旋转
transform :rotate(30deg);旋转 单位deg 默认是Z轴旋转
1、transform :rotate(30deg);默认是按照Z轴旋转。
2、transform :rotateX(30deg);根据X轴给定的参数,从当前元素位置旋转。
3、transform :rotateY(30deg);根据Y轴给定的参数,从当前元素位置旋转。
4、transform :rotateX(30deg) rotateY(30deg) ;
六、scale缩放
transform :scale(2);X和Y同时缩放 默认值是1
1、transform :scale(2);默认是X和Y同时缩放。
2、transform :scaleX(2);通过设置 X 轴的值来定义缩放转换
3、transform :scaleY(3);通过设置 Y 轴的值来定义缩放转换。
4、transform :scale(2,5);定义 2D 缩放。
5、transform :scaleX(2) scaleY(3) ;该元素增加或减少的大小,取决于宽度(X轴)和高度(Y轴)的参数,可以取负值。只不过取负值时,会先让元素进行翻转(顺时针180deg),然后在进行缩放
七、sken倾斜
transform :skew(30deg);倾斜 单位deg
1、transform:skew(30deg);默认是X轴倾斜。
2、transform :skewX(30deg);通过设置 X 轴的值来定义倾斜转换
3、transform :skewY(30deg);通过设置 Y 轴的值来定义倾斜转换
4、transform :skew(30deg,130deg);定义 2D 倾斜
5、transform :skewX(30deg) skewY(130deg) ;
八、
transform :matrix();矩阵
1、transform :matrix(n,n,n,n,n,n);定义 2D 转换,使用六个值的矩阵。
transform :perspective();景深 视距 给到元素本身上
transform :perspective(200px);景深,用来设置用户的眼睛距离元素的距离,数值越大
表示距离越远,看到元素的变化就不明显。不能给负值
九、转换基点
改变元素基点的位置transform-origin
他的主要作用就是让我们在进行transform动作之前可以改变元素的基点位置。
A、transform-origin(X,Y):用来设置元素的运动的基点(参照点)。默认点是元素的中心点。其中X和Y的值可以是百分值,em,px,其中X也可以是字符参数值left,center,right;Y和X一样除了百分值外还可以设置字符值top,center,bottom。
B、transform-origin(X,Y,Z);其中的Z轴的设置,只能是数值。
附注1、
<!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>
*{
padding: 0;margin: 0;
}
div{
width: 295px;
height: 246px;
position: relative;
border: 1px solid red;
}
img{
position: absolute;
top: 5px;
left: 5px;
/* transition: 1s; */
width: 290px;
z-index: 2;
transition: 1s;
}
div:hover img{
top: 100px;
left: 100px;
width: 50px;
}
.a{
position: absolute;
top: 120px;
left:100px ;
transition: 1s;
z-index: 1;
}
div:hover .a{
top: 10px;
}
.b{
position: absolute;
top: 20px;
left: 100px;
z-index: 1;
transition: 1s;
}
div:hover .b{
top:200px;
}
</style>
</head>
<body>
<div>
<p class="a">阿斯顿</p>
<img src="../015day.1上课练习/img/image01.jpg" >
<p class="b">手动阀</p>
</div>
</body>
</html>
附注2、
<!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>
*{
padding: 0;margin: 0;
}
section{
width:640px;
height: 400px;
border: 1px solid red;
margin: 0 auto;
overflow: hidden;
}
p{
text-align: center;
transition: 2s;
}
img{
transition: 2s;
}
section:hover p{
transform: translate(0,-200px);
}
section:hover img{
transform: scale(1.3);
}
</style>
</head>
<body>
<section>
<div>
<img src="../015day.1上课练习/img/1.jpg" alt="">
</div>
<p>撒打算打算</p>
</section>
</body>
</html>
附注3、折扇
<!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>
*{
padding: 0;margin: 0;
}
section{
width: 500px;
height: 400px;
border: 1px solid red;
margin: 100px auto;
position: relative;
}
div{
width: 40px;
height: 200px;
background: pink;
position: absolute;
top: 40px;
left: 230px;
transform-origin: bottom center;
transition: 2s;
opacity: 0.5;
border-radius: 50px 50px 50px 50px;
}
section div:nth-child(1){
background-color: purple;
}
section div:nth-child(2){
background-color: pink;
}
section div:nth-child(3){
background-color: green;
}
section div:nth-child(4){
background-color: lawngreen;
}
section div:nth-child(5){
background-color: lightblue;
}
section div:nth-child(6){
background-color: lightgrey;
}
section div:nth-child(7){
background-color: lightpink;
}
section div:nth-child(8){
background-color: lightcyan;
}
section div:nth-child(9){
background-color: yellow;
}
section div:nth-child(10){
background-color: yellowgreen;
}
section div:nth-child(11){
background-color: lightsalmon;
}
section div:nth-child(12){
background-color: orange;
}
section div:nth-child(13){
background-color: orchid;
}
section:hover div:nth-child(1){
transform: rotate(90deg);
opacity: 1;
}
section:hover div:nth-child(2){
transform: rotate(75deg);
opacity: 1;
}
section:hover div:nth-child(3){
transform: rotate(60deg);
opacity: 1;
}
section:hover div:nth-child(4){
transform: rotate(45deg);
opacity: 1;
}
section:hover div:nth-child(5){
transform: rotate(30deg);
opacity: 1;
}
section:hover div:nth-child(6){
transform: rotate(15deg);
opacity: 1;
}
section:hover div:nth-child(7){
transform: rotate(0deg);
opacity: 1;
}
section:hover div:nth-child(8){
transform: rotate(-15deg);
opacity: 1;
}
section:hover div:nth-child(9){
transform: rotate(-30deg);
opacity: 1;
}
section:hover div:nth-child(10){
transform: rotate(-45deg);
opacity: 1;
}
section:hover div:nth-child(11){
transform: rotate(-60deg);
opacity: 1;
}
section:hover div:nth-child(12){
transform: rotate(-75deg);
opacity: 1;
}
section:hover div:nth-child(13){
transform: rotate(-90deg);
opacity: 1;
}
</style>
</head>
<body>
<section>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</section>
</body>
</html>
附注4、正方体的两种做法
第一种(这种是去正方体的中间切面为基准,然后6个面都是通过中间的面平移加旋转得到的)
<!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>
*{
padding: 0;margin: 0;
}
section{
width: 400px;
height: 400px;
/* background-color: pink; */
margin:200px auto 0 ;
transform: rotatex(20deg) rotatey(30deg);
position: relative;
transform-style: preserve-3d;
}
div{
width: 400px;
height: 400px;
position: absolute;
text-align: center;
line-height: 400px;
font-size: 100px;
}
div:nth-child(1){
transform:translateZ(200px);
background-color: powderblue;
opacity: 0.5;
}
div:nth-child(2){
transform:translateZ(-200px) rotatey(180deg);
background-color: red;
opacity: 0.5;
}
div:nth-child(3){
transform:translatex(-200px) rotatey(-90deg);
background-color: greenyellow;
opacity: 0.5;
}
div:nth-child(4){
transform:translatex(200px) rotatey(90deg);
background-color: burlywood;
opacity: 0.5;
}
div:nth-child(5){
transform:translatey(-200px) rotatex(90deg);
background-color: blue;
opacity: 0.5;
}
div:nth-child(6){
transform:translatey(200px) rotatex(-90deg);
background-color: pink;
opacity: 0.5;
}
</style>
</head>
<body>
<section>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</section>
</body>
</html>
第二种(这种方法是以数字1的面为基准,然后通过平移和以1面的其中一个边为基准通过旋转获得了)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
padding: 0;margin: 0;
}
section{
width: 400px;
height: 400px;
margin: 300px auto 0;
transform: rotatex(30deg) rotatey(20deg);
position: relative;
transform-style: preserve-3d;
background-color: lightskyblue;
}
div{
width: 400px;
height: 400px;
background-color: pink;
position: absolute;
text-align: center;
line-height: 400px;
font-size: 100px;
}
div:nth-child(1){
background-color: red;
opacity: 0.5;
}
div:nth-child(2){
transform:translatez(400px);
background-color: yellow;
opacity: 0.8;
}
div:nth-child(3){
transform:rotatey(-90deg);
transform-origin: left center;
background-color: blue;
opacity: 0.8;
}
div:nth-child(4){
transform:rotatey(90deg);
transform-origin: right center;
background-color: greenyellow;
opacity: 0.5;
}
div:nth-child(5){
transform:rotatex(90deg);
transform-origin: top center;
background-color: pink;
opacity: 0.5;
}
div:nth-child(6){
transform:rotatex(-90deg);
transform-origin: bottom center;
background-color: purple;
opacity: 0.8;
}
</style>
</head>
<body>
<section>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</section>
</body>
</html>