目录
- Transform(变形)属性
- 2D转换
- rotate() 旋转
- translate() 平移
- scale() 缩放
- skew() 扭曲/斜切
- 3D转换
- rotate3d()
- translate3d()---增加了z轴方向平移
- scale3d()---增加了z轴方向缩放
- transform 与 坐标系统
- css3矩阵
- css3扩展属性
- transform-style属性
- perspective属性
*** Transform(变形)属性 ***
-
2D转换
-
rotate() 旋转
【注意】:rotate指定对象的2D旋转,指定对象指的是当前对象,也就是原元素,虽然子元素也会跟着动,但是子元素并没有发生相对旋转,旋转一定要有一个旋转中心,所以需先有transform-origin属性的定义来确定旋转中心,如果缺省,默认为transform-origin: 50% 50%。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>rotate</title>
<style type="text/css">
* { margin: 0; padding: 0; list-style-type: none; }
a,
img { border: 0; }
body { font: 12px/180% Arial; }
.main { width: 1000px; margin: 50px auto; position: relative; }
.pic { width: 300px; height: 290px; border: 1px solid #ccc; background: #fff; box-shadow: 2px 2px 3px #aaa; }
.pic img { margin: 10px 0 0 8px; width: 285px; }
.pic p { text-align: center; font-size: 20px; }
.pic1 {
-webkit-transform: rotate(7deg);
-moz-transform: rotate(7deg);
-ms-transform: rotate(7deg);
-o-transform: rotate(7deg);
transform: rotate(7deg);
}
.pic2 {
-webkit-transform: rotate(-8deg);
-moz-transform: rotate(-8deg);
-ms-transform: rotate(-8deg);
-o-transform: rotate(-8deg);
transform: rotate(-8deg);
}
.pic3 { position: absolute; top: 40px; left: 350px; z-index: 2;
-webkit-transform: rotate(-35deg);
-moz-transform: rotate(-35deg);
-ms-transform: rotate(-35deg);
-o-transform: rotate(-35deg);
transform: rotate(-35deg);
}
.pic4 { position: absolute; top: 360px; left: 350px; z-index: 3;
-webkit-transform: rotate(35deg);
-moz-transform: rotate(35deg);
-ms-transform: rotate(35deg);
-o-transform: rotate(35deg);
transform: rotate(35deg);
}
.pic5 { position: absolute; top: 150px; left: 600px; z-index: 4;
-webkit-transform: rotate(60deg);
-moz-transform: rotate(60deg);
-ms-transform: rotate(60deg);
-o-transform: rotate(60deg);
transform: rotate(60deg);
}
.pic6 { position: absolute; top: 180px; left: 280px; z-index: 5;
-webkit-transform: rotate(-60deg);
-moz-transform: rotate(-60deg);
-ms-transform: rotate(-60deg);
-o-transform: rotate(-60deg);
transform: rotate(-60deg);
}
</style>
</head>
<body>
<div class="main">
<div class="pic pic1"><img src="images/1.jpg"><p>顺时针7度</p></div>
<div class="pic pic2"><img src="images/2.jpg"><p>逆时针8度</p></div>
<div class="pic pic3"><img src="images/3.jpg"><p>逆时针35度</p></div>
<div class="pic pic4"><img src="images/4.jpg"><p>顺时针35度</p></div>
<div class="pic pic5"><img src="images/5.jpg"><p>顺时针60度</p></div>
<div class="pic pic6"><img src="images/6.jpg"><p>逆时针60度</p></div>
</div>
</body>
</html>
-
translate() 平移
//第一个参数必填;如果只写一个参数,则默认第二个参数为0
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>translateX</title>
<style type="text/css">
div { width: 1500px; height: 250px; background: #abcdef; margin: auto; }
div > img {
transform: translateX(200px/em/vh/vm/%...(只能是具体的值,但是不能是left等方位词));
transform: translateY(200px/em/vh/vm/%...(只能是具体的值,但是不能是top等方位词));
transform: translate(200px,100px);
}
</style>
</head>
<body>
<div><img src="images/1.jpg"></div>
</body>
</html>
-
scale() 缩放
// 不需要单位,数值仅能是0.几,不能是百分数
// 第一个参数必填,第二个不写默认与第一个参数相同
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>scaleX</title>
<style type="text/css">
div { width: 1500px; height: 250px; background: #abcdef; margin: auto; }
div > img {
transform: scaleX(.5); /*水平方向 基于中间线缩放为原来的50%,1-100%,...*/
transform: scaleY(.5); /*竖直方向 基于中间水平线缩放*/
}
</style>
</head>
<body>
<!--验证-->
<div><img src="images/1.jpg"></div>
</body>
</html>
-
skew() 扭曲/斜切
【注意】:正数为逆时针斜切;负数为顺时针斜切;中心为图片的中心;(与rotate是相反的)
// 当为90度时,图片会小时
// 当只有一个参数时,第二个参数默认为0
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>skewX</title>
<style type="text/css">
div { width: 1500px; height: 250px; background: #abcdef; margin: auto; }
div > img {
transform: skewX(15deg);/**/
transform: skewY(15deg);
transform: skew(15deg, 15deg);
}
</style>
</head>
<body>
<div><img src="images/1.jpg"></div>
</body>
</html>
-
3D转换
-
rotate3d()
【注意】:在transform: rotate3d(x, y, z, angle);中,第一个参数表示X轴,第二个参数表示Y轴,第三个参数表示Z轴,其中0为该方向不旋转,大于0为正方向旋转(顺时针),小于0为负方向旋转(逆时针),第四个参数表示旋转的角度,参数不允许省略。(rotate3d(0,0,1,-45deg) = rotate3d(0,0,-1,45deg))
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>rotateX</title>
<style type="text/css">
div { width: 1500px; height: 250px; background: #abcdef; margin: auto; }
div > img {
transform: rotateX(45deg);
transform: rotateY(45deg);
transform: rotateZ(45deg);
transform: rotate3d(1, 1, 1, 45deg);
}
</style>
</head>
<body>
<div><img src="images/1.jpg"></div>
</body>
</html>
-
translate3d()---增加了z轴方向平移
// 视觉上不太明显,适用于 遮罩应用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>translateZ</title>
<style type="text/css">
div { width: 1500px; height: 250px; background: #abcdef; margin: auto; }
div > img {
transform: translateZ(200px);
transform: translate3d(200px, 200px, 200px);
}
</style>
</head>
<body>
<div><img src="images/1.jpg"></div>
</body>
</html>
-
scale3d()---增加了z轴方向缩放
// 视觉上没有效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>scaleZ</title>
<style type="text/css">
div { width: 1500px; height: 250px; background: #abcdef; margin: auto; }
div > img {
transform: scaleZ(.5);
transform: scale3d(.5, .5, .5); /*如若不想缩放,则为1*/
}
</style>
</head>
<body>
<div><img src="images/sprite.jpg"></div>
</body>
</html>
*** transform 与 坐标系统 *** (简单说:围绕哪个点转换)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>origin</title>
<style type="text/css">
div { width: 1500px; height: 250px; background: #abcdef; margin: auto; }
div > img {
transform: rotate3d(1, 1, 1, 45deg);
transform-origin: left top 0; /*原元素的左上角*/
}
</style>
</head>
<body>
<div><img src="images/1.jpg"></div>
</body>
</html>
*** css3矩阵 ***(了解,后续用到再补充吧~)
*** css3扩展属性 ***
因为之前3d效果中部分效果不明显,所以引入以下属性
-
transform-style属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>transform style</title>
<style type="text/css">
body { background: #abcdef; }
div { position: relative; width: 760px; height: 760px; margin: auto;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-ms-transform-style: preserve-3d;
-o-transform-style: preserve-3d;
transform-style: preserve-3d;
}
div > .inner { position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; margin: auto; transform: rotateY(-45deg); background: url(images/circle_inner.png) no-repeat center center; }
div > .middle { position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; margin: auto; transform: rotateX(-45deg); background: url(images/circle_middle.png) no-repeat center center; }
div > .outer { position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; margin: auto; transform: rotateZ(-45deg); background: url(images/circle_outer.png) no-repeat center center; }
div > .pig{ position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; margin: auto; background: url(images/pig.png) no-repeat center center; }
</style>
</head>
<body>
<div>
<div class="inner"></div>
<div class="middle"></div>
<div class="outer"></div>
<div class="pig"></div>
</div>
</body>
</html>
-
perspective属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>perspective</title>
<style type="text/css">
body { background: #abcdef; }
div { position: relative; width: 760px; height: 760px; margin: auto;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-ms-transform-style: preserve-3d;
-o-transform-style: preserve-3d;
transform-style: preserve-3d;
-webkit-perspective: 500px;
-moz-perspective: 500px;
-ms-perspective: 500px;
-o-perspective: 500px;
perspective: 500px; /*数值越大越远,数值越小越近*/
}
div > .inner { position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; margin: auto; transform: rotateY(45deg); background: url(images/circle_inner.png) no-repeat center center; }
div > .middle { position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; margin: auto; transform: rotateX(45deg); background: url(images/circle_middle.png) no-repeat center center; }
div > .outer { position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; margin: auto; transform: rotateZ(45deg); background: url(images/circle_outer.png) no-repeat center center; }
div > .pig{ position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; margin: auto; background: url(images/pig.png) no-repeat center center; }
</style>
</head>
<body>
<div>
<div class="inner"></div>
<div class="middle"></div>
<div class="outer"></div>
<div class="pig"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>perspective</title>
<style type="text/css">
body { background: #abcdef; }
div { position: relative; width: 760px; height: 760px; margin: auto;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-ms-transform-style: preserve-3d;
-o-transform-style: preserve-3d;
transform-style: preserve-3d;
-webkit-perspective: 500px;
-moz-perspective: 500px;
-ms-perspective: 500px;
-o-perspective: 500px;
perspective: 500px; /*数值越大越远,数值越小越近*/
-webkit-perspective-origin: bottom;
-moz-perspective-origin: bottom;
-ms-perspective-origin: bottom;
-o-perspective-origin: bottom;
perspective-origin: bottom;
}
div > .inner { position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; margin: auto; transform: rotateY(45deg); background: url(images/circle_inner.png) no-repeat center center; }
div > .middle { position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; margin: auto; transform: rotateX(45deg); background: url(images/circle_middle.png) no-repeat center center; }
div > .outer { position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; margin: auto; transform: rotateZ(45deg); background: url(images/circle_outer.png) no-repeat center center; }
div > .pig{ position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; margin: auto; background: url(images/pig.png) no-repeat center center; }
</style>
</head>
<body>
<div>
<div class="inner"></div>
<div class="middle"></div>
<div class="outer"></div>
<div class="pig"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>backface-visibility</title>
<style type="text/css">
body {
-webkit-perspective: 800px;
perspective: 800px;
-webkit-perspective-origin: 50%;
perspective-origin: 50%;
}
.cube { display: inline-block; width: 100px; height: 100px; margin: 50px;
-webkit-transform-style: preserve-3d;
transform-style: preserve-3d;
}
.cube > div { position: absolute; width: 100%; height: 100%; box-shadow: inset 0 0 15px rgba(0, 0, 0, .2); background-color: rgba(255, 255, 255, .1); color: gray; font-size: 20px; line-height: 100px; text-align: center; }
.front {
-webkit-transform: translatez(50px);
transform: translatez(50px);
}
.back {
-webkit-transform: rotatey(180deg) translatez(50px);
transform: rotatey(180deg) translatez(50px);
}
.right {
-webkit-transform: rotatey(90deg) translatez(50px);
transform: rotatey(90deg) translatez(50px);
}
.left {
-webkit-transform: rotatey(-90deg) translatez(50px);
transform: rotatey(-90deg) translatez(50px);
}
.top {
-webkit-transform: rotatex(90deg) translatez(50px);
transform: rotatex(90deg) translatez(50px);
}
.bottom {
-webkit-transform: rotatex(-90deg) translatez(50px);
transform: rotatex(-90deg) translatez(50px);
}
.c1 > div {
-webkit-backface-visibility: visible;
backface-visibility: visible;
}
.c2 > div {
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
}
</style>
</head>
<body>
<div class="cube c1">
<div class="front">1</div>
<div class="back">2</div>
<div class="right">3</div>
<div class="left">4</div>
<div class="top">5</div>
<div class="bottom">6</div>
</div>
<div class="cube c2">
<div class="front">1</div>
<div class="back">2</div>
<div class="right">3</div>
<div class="left">4</div>
<div class="top">5</div>
<div class="bottom">6</div>
</div>
</body>
</html>