声明:以下内容为个人学习总结,初衷是方便自己学习复习记录。如有错误之处,烦请多多指正!
动画的使用方法
- 定义动画
- 调用
案例1:使气球转一圈
实现代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
img {
width: 100px;
/* 调用动画 */
animation-name: move;
/* 动画持续时间 */
animation-duration: 10s;
/*动画被播放次数,默认是1(infinite表示无限次) */
animation-iteration-count: infinite;
}
/* 动画的定义 */
@keyframes move {
/* 开始 */
0% {
transform: translate(0px, 0px);
}
25% {
transform: translate(1000px, 0px);
}
50% {
transform: translate(1000px, 500px);
}
75% {
transform: translate(0px, 500px);
}
/* 结束 */
100% {
transform: translate(0px, 0px);
}
}
</style>
</head>
<body>
<div>
<img src="imgs/hop.png" alt="">
</div>
</body>
</html>
案例2:奔跑的熊
实现代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
background-color: #ccc;
}
.bear {
position: absolute;
width: 200px;
height: 100px;
background: url(imgs/bear.png) no-repeat;
/* 动画1bear:图片长度是1600px,共8个动作,所以盒子宽度设为200px,分8步动画实现熊走动;
动画2move:移动到屏幕中央,保持位置 */
animation: bear 5s steps(8) infinite, move 5s forwards;
}
@keyframes bear {
0% {
background-position: 0 0;
}
100% {
background-position: -1600px 0;
}
}
@keyframes move {
0% {
bottom: 10px;
left: 0px;
}
100% {
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
}
</style>
</head>
<body>
<div class="bear">
</div>
</body>
</html>