一:需求描述
实现一个小人跑步的动画效果
二:实现思路
1.写一个盒子,盒子宽高根据背景图宽进行等分,通过 background-image把图片放入盒子中,再添加动画,让小人在盒子里跑起来
动画实现:move 1s steps(12) infinite, (表示动画持续1s,逐帧分步为12步,无限循环)
2.再添加一个动画,通过 background-position属性让盒子动起来
动画实现: run 2s linear forwards, (表示动画持续2s,匀速,停留在动画结束的状态)
三:代码实现
<!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>小人跑步</title>
<style>
.box{
width:140px;
height:140px;
/* border:1px solid pink; */
background-image: url(./bg.png);
animation:
move 1s steps(12) infinite,
run 2s linear forwards;
}
/* 人在盒子里跑 */
@keyframes move{
/* 若从0 开始的话这个from可以省去 */
/* from{
background-position: 0 0;
} */
to{
background-position: -1680px 0;
}
}
/* 盒子在X轴上有0跑到800px, */
@keyframes run{
/* 若从0 开始的话这个from可以省去 */
/* from{
transform: translateX(0);
} */
to{
transform: translateX(800px);
}
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>