运行结果:
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
body {
background-color: gray;
}
.mountain {
height: 336px;
background: url(img/bg1.png)no-repeat;
animation: mountains 5s infinite linear;
}
.bear {
position: absolute;
top: 100px;
width: 200px;
height: 100px;
background: url(img/bear.png) no-repeat;
animation: bears 1s steps(8) infinite, move 3s forwards;
}
@keyframes bears {
0% {
background-position: 0 0;
}
100% {
background-position: -1600px 0;
}
}
@keyframes move {
0% {
left: 0;
}
100% {
left: 50%;
transform: translate(-50%);
}
}
@keyframes mountains {
0% {
background-position: 0, 0;
}
100% {
background-position: -2300px, 0;
}
}
</style>
<body>
<div class="mountain"></div>
<div class="bear"></div>
</body>
</html>