简体   繁体   English

使用CSS3结束关键帧动画

[英]End Keyframe Animation Using CSS3

I've created a simple bar animation using keyframes in CSS and applying it to an id tag 我使用CSS中的关键帧创建了一个简单的条形动画,并将其应用于id标签

 #progress-skill { animation: color-bar 2s, animate-bar 2s; animation-fill-mode: forwards; position: relative; border-radius: 3px; width: 400px; } @keyframes color-bar { 5% { background-color: red;} 25% { background-color: orange;} 50% { background-color: yellow;} 75% { background-color: green;} 100% { background-color: blue;} } @keyframes animate-bar { from {width: 0px; } to {width: 200px; } } 
 <div class="progress"> <div class="progress-title"><span>SKILL NAME</span></div> <div class="progress-bar" id="progress-skill"><span>50%</span></div> </div> 

The animation works fine, with the exception that the color will always end up being the 100% background-color no matter what the percentage is. 动画效果很好,但无论百分比是多少,颜色始终总是100%背景色。 Is there any way to force the animation to stop at certain percentages? 有什么方法可以迫使动画停止在特定的百分比? Basically what I'm looking for here is an animation background-color triggered by the width of the bar. 基本上,我在这里寻找的是由条形宽度触发的动画背景色。 If the bar is 50% long then it should be the 50% background-color and stop at that point. 如果条长为50%,则应为50%背景色,然后在该点停止。 The same should hold true for 25% or 75%. 25%或75%的情况相同。

I'm not looking for a javascript solution, but I'm willing to compromise if that's the only method possible. 我不是在寻找JavaScript解决方案,但是如果这是唯一可行的方法,我愿意妥协。

javascript would make it much easier. javascript将使其变得更加容易。 but I started playing with the CSS, and kinda made some progress. 但是我开始使用CSS,并取得了一些进步。 gotta go home now, so i can't finish it. 现在要回家了,所以我做不完。 https://jsfiddle.net/L9bznjLb/ https://jsfiddle.net/L9bznjLb/

@keyframes color-bar {
    from {width: 0px; }
    to {width: 20px; }

I'm pretty sure that you can't stop an animation the way that you are asking. 我很确定您不能按照要求的方式停止播放动画。 My best suggestion would be to create several variations for each percentage, using class names rather than ids, like so: 我最好的建议是使用类名而不是ID为每个百分比创建几个变体,如下所示:

 .progress-bar { position: relative; border-radius: 3px; width: 400px; } .progress-bar-5 { animation: color-bar-5 2s, animate-bar-5 2s; animation-fill-mode: forwards; } .progress-bar-25 { animation: color-bar-25 2s, animate-bar-25 2s; animation-fill-mode: forwards; } .progress-bar-50 { animation: color-bar-50 2s, animate-bar-50 2s; animation-fill-mode: forwards; } .progress-bar-75 { animation: color-bar-75 2s, animate-bar-75 2s; animation-fill-mode: forwards; } .progress-bar-100 { animation: color-bar-100 2s, animate-bar-100 2s; animation-fill-mode: forwards; } @keyframes color-bar-5 { 100% { background-color: red;} } @keyframes color-bar-25 { 20% { background-color: red;} 100% { background-color: orange;} } @keyframes color-bar-50 { 10% { background-color: red;} 50% { background-color: orange;} 100% { background-color: yellow;} } @keyframes color-bar-75 { 7% { background-color: red;} 33% { background-color: orange;} 67% { background-color: yellow;} 100% { background-color: green;} } @keyframes color-bar-100 { 5% { background-color: red;} 25% { background-color: orange;} 50% { background-color: yellow;} 75% { background-color: green;} 100% { background-color: blue;} } @keyframes animate-bar-5 { from {width: 0px; } to {width: 10px; } } @keyframes animate-bar-25 { from {width: 0px; } to {width: 50px; } } @keyframes animate-bar-50 { from {width: 0px; } to {width: 100px; } } @keyframes animate-bar-75 { from {width: 0px; } to {width: 150px; } } @keyframes animate-bar-100 { from {width: 0px; } to {width: 200px; } } 
 <div class="progress"> <div class="progress-title"><span>SKILL NAME</span></div> <div class="progress-bar progress-bar-50"><span>50%</span></div> </div> <div class="progress"> <div class="progress-title"><span>SKILL NAME</span></div> <div class="progress-bar progress-bar-25"><span>25%</span></div> </div> <div class="progress"> <div class="progress-title"><span>SKILL NAME</span></div> <div class="progress-bar progress-bar-75"><span>75%</span></div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM