animation-fill-mode:forwards
是要使用的正确属性。似乎不起作用,因为精灵图像背景有一个 default background-repeat:repeat
,所以你认为你看到的最后一帧实际上是重复背景图像的第一帧。
如果你设置
background: url("http://files.simurai.com/misc/sprite.png") no-repeat
animation: play .8s steps(10) forwards;
@keyframes play {
from { background-position: 0px; }
to { background-position: -500px; }
}
并运行演示,最后一帧现在是空白的 -forwards
正在做它应该做的事情。解决方案的第二部分是更改 finalto
和steps
CSS 属性以正确定位背景。所以我们真的需要背景停下来-450px
使用9
步骤。
-webkit-animation: play .8s steps(9) forwards;
@keyframes play {
from { background-position: 0; }
to { background-position: -450px; }
}
请参阅演示- 我只修复了 Chrome 属性。如果原件消失,这里也是示例图像。
.hi {
width: 50px;
height: 72px;
background: url("http://i.stack.imgur.com/ilKfd.png") no-repeat;
-webkit-animation: play .8s steps(9) forwards;
-moz-animation: play .8s steps(10) infinite;
-ms-animation: play .8s steps(10) infinite;
-o-animation: play .8s steps(10) infinite;
animation: play .8s steps(9) forwards;
}
@-webkit-keyframes play {
from { background-position: 0px; }
to { background-position: -450px; }
}
@-moz-keyframes play {
from { background-position: 0px; }
to { background-position: -500px; }
}
@-ms-keyframes play {
from { background-position: 0px; }
to { background-position: -500px; }
}
@-o-keyframes play {
from { background-position: 0px; }
to { background-position: -500px; }
}
@keyframes play {
from { background-position: 0px; }
to { background-position: -450px; }
}
<div class="hi"></div>