CSS3转换
转换:元素的位移、旋转、缩放
6.1 2D转换
- 移动translate
语法:
特点:不会影响其他的盒子
小技巧:实现盒子的水平居中、垂直居中——定位+转换
.fa {
position: relative;
width: 400px;
height: 400px;
background-color: antiquewhite;
}
.son {
position: absolute;
width: 200px;
height: 200px;
background-color: aquamarine;
top: 50%;
left: 50%;
/*往上、往左走本身的一半*/
transform: translate(-50%, -50%);
}
- 旋转rotate
语法:
img {
width: 200px;
height: 200px;
transform: rotate(0deg);
border-radius: 50%;
border: 5px solid #FFEAB2;
/* 过渡效果 */
transition: all 1s;
}
img:hover {
transform: rotate(360deg);
}
小技巧:实现三角
div {
position: relative;
width: 150px;
height: 30px;
border: 1px solid #000;
}
div::after {
content: '';
top: 5px;
right: 15px;
position: absolute;
width: 15px;
height: 15px;
border-right: 1px solid #000;
border-bottom: 1px solid #000;
transform: rotate(45deg);
transition: all 1s;
}
div:hover::after {
/*实现上下翻转*/
transform: rotate(225deg);
}
- 转换中心点transform- origin
语法:
练习:
div {
overflow: hidden;
position: relative;
width: 150px;
height: 150px;
border: 2px solid #FBBC03;
margin: 100px auto;
}
div::before {
top: 0;
left: 0;
position: absolute;
content: 'TNT';
width: 150px;
height: 150px;
background-color: #FBBC03;
transform: rotate(180deg);
transform-origin: left bottom;
transition: all 7s;
}
div:hover::before {
transform: rotate(0deg);
}
- 缩放scale
语法:
注意:里面的x,y是指的倍数,大于1是放大,小于1是缩小
特点:不影响其他盒子
练习1:图片的缩放
div {
overflow: hidden;
width: 200px;
height: 200px;
margin: 100px auto;
}
div a img {
transition: all .6s;
}
div a img:hover {
transform: scale(1.2);
}
2D转换的综合写法:
transform*: translate(100px, 100px) rotate(45deg) scale(1.1);
位移一定写在最前面!!
6.2动画
动画:
第一步:定义动画
第二步:调用动画
定义动画:
0%是动画的开始,100%是动画的结束,可以替换为from和to
调用动画:
基本使用:
@keyframes yidong {
0% {
transform: translate(0px);
}
100% {
transform: translate(500px, 0);
}
}
div {
width: 200px;
height: 200px;
background-color: antiquewhite;
animation-name: yidong;
animation-duration: 2s;
}
动画序列:
@keyframes yidong {
0% {
transform: translate(0px);
}
25% {
transform: translate(500px, 0);
}
50% {
transform: translate(500px, 300px);
}
75% {
transform: translate(0, 300px);
}
100% {
transform: translate(0, 0);
}
}
动画的常用属性:
简写:
练习:热点图
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;
}
.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% {
/* transform: scale(5); 我们不要用scale 因为他会让 阴影变大*/
width: 40px;
height: 40px;
opacity: 1;
}
100% {
width: 70px;
height: 70px;
opacity: 0;
}
}
animation-timing-function:动画的速度曲线
奔跑的熊大:
body {
width: 100%;
height: 336px;
position: absolute;
background: url(media/bg1.png) no-repeat;
animation: leftmove 20s linear infinite forwards;
}
div {
position: absolute;
z-index: 3;
top: 200px;
width: 200px;
height: 100px;
background: url(media/bear.png) no-repeat;
animation: bear 2.5s steps(8) infinite, move 7s forwards;
}
@keyframes bear {
0% {
background-position: 0 0;
}
100% {
background-position: -1600px 0;
}
}
@keyframes move {
0% {
left: 0;
}
100% {
left: 50%;
transform: translate(-50%);
}
}
@keyframes leftmove {
0% {
background-position: 0 0;
}
100% {
background-position: 100% 0;
}
}
6.3 3D转换
三维坐标系:
- 3D移动translate3d
transform: translate3d(x,y,z);
- 透视perspective
要加在父盒子上
- 3D旋转rotate3d
遵循左手准则:
/* transform: rotateY(-45deg); */
/* 延x轴旋转 */
/* transform: rotate3d(1, 0, 0, 45deg); */
/* 延对角线旋转 */
transform: rotate3d(1, 1, 0, 45deg);
- 3D呈现transfrom-style
控制子盒子是否开启三维立体环境
transform-style: preserve-3d;//开启3d环境
body {
perspective: 500px;
}
.box {
position: relative;
width: 200px;
height: 200px;
margin: 100px auto;
transform-style: preserve-3d;
transition: all .5s;
}
.box:hover {
transform: rotateY(-45deg);
}
.box div {
position: absolute;
width: 100%;
height: 100%;
background-color: aquamarine;
}
.box div:last-child {
background-color: bisque;
transform: rotateX(60deg);
}
练习:两个盒子翻转
body {
perspective: 400px;
}
.box {
position: relative;
width: 300px;
height: 300px;
margin: 100px auto;
transform-style: preserve-3d;
transition: all 7s;
}
.box:hover {
transform: rotateY(180deg);
}
.front,
.back {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
color: azure;
font-size: 30px;
text-align: center;
line-height: 300px;
border-radius: 50%;
}
.front {
background-color: orange;
z-index: 1;
/* 背面隐藏 背面朝向用户时不可见 默认值是visible(可见) */
backface-visibility: hidden;
}
.back {
background-color: pink;
transform: rotateY(180deg);
}
练习:盒子上下翻转
body {
perspective: 400px;
}
.box {
position: relative;
width: 200px;
height: 100px;
margin: 100px auto;
transform-style: preserve-3d;
transition: all 2s;
}
.box:hover {
transform: rotateX(90deg);
}
.front,
.back {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
color: azure;
font-size: 20px;
text-align: center;
line-height: 100px;
}
.front {
background-color: orange;
z-index: 1;
/* 背面隐藏 背面朝向用户时不可见 默认值是visible(可见) */
backface-visibility: hidden;
transform: translateZ(50px);
}
.back {
background-color: pink;
transform: translateY(50px) rotateX(-90deg);
}
练习:旋转木马
body {
perspective: 1000px;
}
section {
position: relative;
width: 320px;
height: 213px;
margin: 150px auto;
transform-style: preserve-3d;
animation: move 10s linear infinite;
}
section:hover {
animation-play-state: paused;
}
@keyframes move {
0% {
transform: rotateY(0deg);
}
100% {
transform: rotateY(360deg);
}
}
section div {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
background: url(media/tnt.jpeg) no-repeat;
}
section div:first-child {
transform: 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:last-child {
transform: rotateY(300deg) translateZ(300px);
}