html - CSS 动画在结束时重置为第一个关键帧

标签 html css css-animations

我有以下代码,可以对元素的背景位置进行动画处理以创建关键帧动画。它是 12 帧,图像是 1536px。

但是,即使我有 -webkit-animation-fill-mode:forwards; 动画在最后会重置回第一帧...我该如何停止?

HTML:

<div class="sequence animate"></div>

CSS:

div.sequence
{
    width: 128px;
    height: 128px;
    background: url('./img/sequence.png');
}

div.sequence.animate
{
    -webkit-animation: boxopen 4s steps(12);
    -webkit-animation-fill-mode: forwards;
}

@-webkit-keyframes boxopen {
    from {background-position: 0;}
    to {background-position: -1536px;}
}

在这里摆弄:http://jsfiddle.net/zpsa37m3/1/

最佳答案

您的图像会重复,因此到第 12 帧时,图像完全向左偏移,并且您会看到它的开头重复。只需在 11 个步骤后停止动画即可:

div.sequence
{
    width: 128px;
    height: 128px;
    background: url('http://i60.tinypic.com/1otwkg.png');
}

div.sequence.animate
{
    -webkit-animation: boxopen 6s steps(11); /* 11 steps not 12 */
    -webkit-animation-fill-mode: forwards;
}

@-webkit-keyframes boxopen {
    from {background-position: 0;}
    to {background-position: -1408px;} /* stop at the left of frame 12 */
}
<div class="sequence animate"></div>

演示:http://jsfiddle.net/zpsa37m3/3/

关于html - CSS 动画在结束时重置为第一个关键帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27918814/

相关文章:

javascript - 如何让 HTML5 Canvas 显示 HTML?

html - 将输入元素置于 Flex 容器中居中

html - 在 flex 中将 div 的内容右对齐

html - 为什么@keyframe 动画只工作一次?

javascript - 汉堡菜单点击动画问题

css - 在 CSS 媒体查询期间淡出/淡入 DIV

html - 第三级 Accordion 不会打开CSS

html - 表和::第n个 child

html - 如何使用 twitter bootstrap 和 rails 简单表单创建水平表单

html - 在包装之前使 flexbox 缩小尺寸