【发布时间】:2017-01-12 08:00:05
【问题描述】:
我有一个 CSS 关键帧动画,它本身很普通。但是,我希望能够强制动画位于特定帧。 类似anim.setProgress(0.13),它将动画设置为 13%。 我该怎么做?
请注意,我不只是想在任意点开始动画。无论动画有多远,我都希望能够打断它并说“回到 32%”之类的。
【问题讨论】:
标签: css animation css-animations keyframe
我有一个 CSS 关键帧动画,它本身很普通。但是,我希望能够强制动画位于特定帧。 类似anim.setProgress(0.13),它将动画设置为 13%。 我该怎么做?
请注意,我不只是想在任意点开始动画。无论动画有多远,我都希望能够打断它并说“回到 32%”之类的。
【问题讨论】:
标签: css animation css-animations keyframe
给动画一个负的animation-delay 值。对于运行 10 秒的动画,13% 的帧将以-1.3s 为目标。
如果“animation-delay”的值为负时间偏移,则动画将在应用它的那一刻执行,但似乎已在指定的偏移处开始执行。也就是说,动画将在其播放周期的中途开始。如果动画具有隐含的起始值和负的“动画延迟”,则从应用动画的那一刻开始获取起始值。
div.loading {
animation: loading 10s linear 1;
animation-play-state: paused;
animation-delay: -1.3s;
/*start at 13%*/
}
div.ten-seconds {
animation-delay: -1s;
/*start at 10%*/
}
div.zero-seconds {
animation-delay: 0s;
/*start at 0%*/
}
/*Note how the keyframes start at 0 width*/
@keyframes loading {
0% {
width: 0;
}
100% {
width: 100%;
}
}
/*Just for this example below*/
input {
display: none
}
label {
display: block;
background: gray;
width: 120px;
height: 50px;
line-height: 50px;
text-align: center;
font-size: 20px;
margin: 20px 0 20px 0;
color: #FFF;
cursor: pointer;
}
input:checked ~ .loading {
animation-play-state: running;
}
div {
height: 100px;
background: pink;
}
div.ruler {
width: 13%;
height: 20px;
line-height: 20px;
text-align: center;
color: #FFF;
background: gray;
border-bottom: solid 2px #000;
}
div.ruler.bottom {
width: 10%;
}
<input type="checkbox" id="start" />
<label for="start">Start / Pause</label>
<div class="ruler">Go 13%</div>
<div class="loading"></div>
<div class="ruler bottom">Go 10%</div>
<div class="loading ten-seconds"></div>
<h2>No delay below</h2>
<div class="loading zero-seconds"></div>
【讨论】: