transform变换
CSS3 transform变换
1、translate(x,y) 设置盒子位移
2、scale(x,y) 设置盒子缩放
3、rotate(deg) 设置盒子旋转
4、skew(x-angle,y-angle) 设置盒子斜切
5、perspective 设置透视距离
6、transform-style flat | preserve-3d 设置盒子是否按3d空间显示
7、translateX、translateY、translateZ 设置三维移动
8、rotateX、rotateY、rotateZ 设置三维旋转
9、scaleX、scaleY、scaleZ 设置三维缩放
10、tranform-origin 设置变形的中心点
11、backface-visibility 设置盒子背面是否可见
transform是一个静态的效果,不是做动画的;但可以和动画进行配合使用;
其中,旋转用的比较多;
位移,缩放,旋转,斜切示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>transform</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.box {
width: 200px;
height: 200px;
border: 3px solid black;
margin: 50px auto 0;
background-color: aqua;
/* translate位移一比定位做的位移性能高,建议使用这种; */
transform: translate(0px, 0px);
transition: all 500ms ease; /* 通过transition将下面hover的效果实现动画化 */
}
.box:hover {
transform: translate(30px, 30px);
}
.box2 {
width: 200px;
height: 200px;
border: 3px solid black;
margin: 50px auto 0;
background-color: aqua;
transform: scale(1,1); /* 垂直缩放 */
transition: all 500ms ease; /* 通过transition将下面hover的效果实现动画化 */
}
.box2:hover {
transform: scale(2,2);
}
.box3 {
width: 200px;
height: 200px;
border: 3px solid black;
margin: 50px auto 0;
background-color: aqua;
transform: rotate(0deg); /* 旋转 */
transition: all 500ms ease; /* 通过transition将下面hover的效果实现动画化 */
}
.box3:hover {
transform: rotate(45deg);
}
.box4 {
width: 200px;
height: 200px;
border: 3px solid black;
margin: 50px auto 0;
background-color: aqua;
transform: skew(0,0); /* 斜切,将图形倾斜 */
transition: all 500ms ease; /* 通过transition将下面hover的效果实现动画化 */
}
.box4:hover {
transform: skew(45deg,0);
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</body>
</html>
位移,缩放,旋转,斜切示例
transform-origin变换中心示例
transform-origin变换中心示例
图片翻转示例
![花的图片](../../images/location_bg.jpg)
图片翻转示例
翻面动画示例
![花的图片](../../images/location_bg.jpg)
linear 匀速
ease 开始和结束慢速
ease-in 开始是慢速
ease-out 结束时慢速
ease-in-out 开始和结束时慢速
steps 动画步数
5、animation-delay 动画延迟
6、animation-iteration-count 动画播放次数 n|infinite
7、animation-direction
normal 默认动画结束不返回
alternate 动画结束后返回
8、animation-play-state 动画状态
paused 停止
running 运动
9、animation-fill-mode 动画前后的状态
none 不改变默认行为
forwards 当动画完成后,保持最后一个属性值(在最后一个关键帧中定义)
backwards 在 animation-delay 所指定的一段时间内,在动画显示之前,应用开始属性值(在第一个关键帧中定义)
both 向前和向后填充模式都被应用
10、animation:name duration timing-function delay iteration-count direction;同时设置多个属性
animation使用示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>animation动画</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
/* 定义动画 */
@keyframes moving {
from {
width: 100px;
}
to {
width: 500px;
}
}
.box {
width: 100px;
height: 100px;
background-color: aqua;
/*animation: moving 1s ease;*/
/*animation: moving 1s ease 1s; !*延迟一秒*!*/
/*animation: moving 1s ease 0s 5; !*反复五次,可以不写延迟*!*/
/*animation: moving 1s ease 0s 5 alternate; !*反复五次,并且返回,来回算两次*!*/
animation: moving 1s ease 0s infinite alternate; /*反复无数次*/
animation-play-state: paused; /*默认不移动*/
}
.box:hover {
/*animation-play-state: paused; !*鼠标放在图形上停止移动,拿走继续移动*!*/
animation-play-state: running; /*鼠标放在图形上开始移动,拿走不移动*/
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
animation使用示例
风车旋转示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>风车旋转</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
@keyframes rotating {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.cycle {
display: block;
width: 400px;
height: 400px;
margin: 50px auto 0;
animation: rotating 1s linear infinite;
}
</style>
</head>
<body>
<img src="./images/fengche.png" alt="风车图片" class="cycle">
</body>
</html>
风车旋转示例
风车图片
loading动画的制作
css3新增选择器,不用写过多的class,直接用某某下的某某的第几个即可;
loading动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>等待动画</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
@keyframes loading {
from {
transform: scale(1, 1);
}
to {
transform: scale(1, 0.5);
}
}
.con {
width: 300px;
height: 158px;
border: 2px solid black;
margin: 50px auto 0;
}
.con div {
width: 30px;
height: 100px;
float: left;
background-color: aqua;
margin: 15px;
border-radius: 15px;
animation: loading 500ms ease 0s infinite alternate;
}
.con div:nth-child(1) {background-color: red;}
.con div:nth-child(2) {background-color: green; animation-delay: 200ms}
.con div:nth-child(3) {background-color: blue; animation-delay: 400ms}
.con div:nth-child(4) {background-color: aqua; animation-delay: 600ms}
.con div:nth-child(5) {background-color: gray; animation-delay: 800ms}
.con p {text-align: center}
</style>
</head>
<body>
<div class="con">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<p>loading...</p>
</div>
</body>
</html>
loading动画
走路动画
animation步数的应用-走路动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>走路动画</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
@keyframes walking {
from {
left: 0;
}
to {
left: -960px;
}
}
.box {
width: 120px;
height: 180px;
border: 2px solid black;
margin: 50px auto 0;
overflow: hidden;
position: relative;
}
.box img {
position: absolute;
left: 0;
top: 0;
animation: walking 1s steps(8) infinite;
}
</style>
</head>
<body>
<div class="box">
<img src="./images/walking.png" alt="走路图片">
</div>
</body>
</html>
走路动画示例
走路图片
最后,给大家推荐一个前端学习进阶内推交流圈子(前端资料分享),不管你在地球哪个方位,
不管你参加工作几年都欢迎你的入驻!(会定期免费提供一些收藏的免费学习书籍资料以及整理好的面试题和答案文档!)
如果您对这个文章有任何异议,那么请在文章评论处写上你的评论。
如果您觉得这个文章有意思,那么请分享并转发,或者也可以关注一下表示您对我们文章的认可与鼓励。
愿大家都能在编程这条路,越走越远。