平面转换(2D转换)transform
平移
<!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 {
height: 100px;
width: 100px;
background-color: pink;
transition: all 2s;
/* transform: translate(0px, 0px); */
}
div:hover {
transform: translate(100px, 100px);
background-color: green;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
盒子居中案例
定位+边距居中
<!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>
.father {
position: relative;
margin: 100px auto;
width: 600px;
height: 600px;
background-color: red;
}
.son {
position: absolute;
top: 50%;
left: 50%;
width: 200px;
height: 200px;
background-color: pink;
margin-top: -100px;
margin-left: -100px;
/* transform: translate(-50%, -50%); */
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
定位+平移居中
<!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>
.father {
position: relative;
margin: 100px auto;
width: 600px;
height: 600px;
background-color: red;
}
.son {
position: absolute;
top: 50%;
left: 50%;
width: 200px;
height: 200px;
background-color: pink;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
旋转
语法:transform:rotate(deg)
旋转原点:transform-origin:X Y;
<!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 {
display: block;
transition: 1000s;
margin: 10px auto;
border: 1px solid #000;
transform-origin: 100% 100%;
}
img:hover {
transform: rotate(360000deg);
}
</style>
</head>
<body>
<div>
<img src="./rotate.png" alt="">
</div>
</body>
</html>
旋转加移动组合
transform:translate(X,Y) rotade (deg)
<!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 {
display: block;
border: 2px solid #000;
margin: 100px auto;
width: 1200px;
}
img {
transition: all 2s;
}
div:hover img {
transform: translateX(900px) rotate(360deg);
}
</style>
</head>
<body>
<div>
<img src="./tyre.png" alt="">
</div>
</body>
</html>
(一定要是先平移再旋转,顺序不能交换)
缩放
transform:scale(倍数)
可以单独改变X或者Y轴上的尺寸
transform:scale
<!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>
/* 给盒子宽度,高度让内容撑开 */
.box {
width: 250px;
margin: 100px auto;
border: 1px solid #000;
}
.pic {
position: relative;
overflow: hidden;
}
/* 先把图片透明度改为0,相当于看不见,然后修改大小和位置 */
.play {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(5);
opacity: 0;
transition: all 2s;
}
/* 鼠标移过来后改变大小,透明度改为1,这样子就可以跟原图一样了 */
.box:hover .play {
transform: translate(-50%, -50%) scale(1);
opacity: 1;
}
</style>
</head>
<body>
<div class="box">
<div class="pic">
<img src="./images/party.png" alt="">
<img src="./images/play.png" alt="" class="play">
</div>
<p>【和平精英】“初火”音乐概念片:四圣觉醒..... </p>
</div>
</body>
</html>
单标签不能添加伪元素
渐变
background-image:linear-gradient(color1,color2)
<!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: 200px;
height: 200px;
background-image: linear-gradient(green, red);
}
</style>
</head>
<body>
<div></div>
</body>
</html>