简体   繁体   English

使用关键帧创建循环动画

[英]Using keyframes to create cyclic animation

I am working on a CSS only slider. 我正在开发一个仅限CSS的滑块。 However I don't have much experience with using keyframes. 但是我没有太多使用关键帧的经验。

I found this pen; 我找到了这支笔; could someone explain to me how the keyframes ensure that the animation runs in a cyclic manner rather than at the same time (where all the slides would disappear and reappear together)? 有人可以向我解释关键帧如何确保动画以循环方式运行而不是同时运行(所有幻灯片将消失并重新出现在一起)?

Code pen link 代码笔链接

 .slider { max-width: 300px; height: 200px; margin: 20px auto; position: relative; } .slide1, .slide2, .slide3, .slide4, .slide5 { position: absolute; width: 100%; height: 100%; } .slide1 { background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371566801_p17tbs0rrjqdt1u4dnk94fe4b63.jpg)no-repeat center; background-size: cover; animation: fade 8s infinite; -webkit-animation: fade 8s infinite; } .slide2 { background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371565525_p17tbqpu0d69c21hetd77dh483.jpeg)no-repeat center; background-size: cover; animation: fade2 8s infinite; -webkit-animation: fade2 8s infinite; } .slide3 { background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371564896_p17tbq6n86jdo3ishhta3fv1i3.jpg)no-repeat center; background-size: cover; animation: fade3 8s infinite; -webkit-animation: fade3 8s infinite; } @keyframes fade { 0% { opacity: 1 } 33.333% { opacity: 0 } 66.666% { opacity: 0 } 100% { opacity: 1 } } @keyframes fade2 { 0% { opacity: 0 } 33.333% { opacity: 1 } 66.666% { opacity: 0 } 100% { opacity: 0 } } @keyframes fade3 { 0% { opacity: 0 } 33.333% { opacity: 0 } 66.666% { opacity: 1 } 100% { opacity: 0 } } 
 <div class='slider'> <div class='slide1'></div> <div class='slide2'></div> <div class='slide3'></div> </div> 

This is due to the each slide using a different keyframe, ie slide1 uses fade , slide2 uses fade2 and slide3 uses fade3 . 这是由于每个幻灯片使用不同的关键帧,即slide1使用fadeslide2使用fade2slide3使用fade fade3 These keyframes all last 8 seconds however the frame in which the slide is shown is different: 这些关键帧都持续8秒,但显示幻灯片的帧不同:

  • slide1 is shown at 0% (0 seconds) slide1显示为0%(0秒)
  • slide2 is shown at 33.333% (about 2.6 seconds) slide2显示为33.333%(约2.6秒)
  • slide3 is shown at 66.666% (about 5.3 seconds) slide3显示为66.666%(约5.3秒)

This particular method will work when you have three slides but would need to be adapted if you were to have a different amount. 当您有三张幻灯片时,此特定方法将起作用,但如果您有不同的金额,则需要进行调整。 For example with four you would need to add an extra step to the keyframe: 例如,对于四个,您需要向关键帧添加额外的步骤:

 .slider { max-width: 300px; height: 200px; margin: 20px auto; position: relative; } .slide1, .slide2, .slide3, .slide4, .slide5 { position: absolute; width: 100%; height: 100%; } .slide1 { background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371566801_p17tbs0rrjqdt1u4dnk94fe4b63.jpg)no-repeat center; background-size: cover; animation: fade 8s infinite; -webkit-animation: fade 8s infinite; } .slide2 { background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371565525_p17tbqpu0d69c21hetd77dh483.jpeg)no-repeat center; background-size: cover; animation: fade2 8s infinite; -webkit-animation: fade2 8s infinite; } .slide3 { background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371564896_p17tbq6n86jdo3ishhta3fv1i3.jpg)no-repeat center; background-size: cover; animation: fade3 8s infinite; -webkit-animation: fade3 8s infinite; } .slide4 { background: red; background-size: cover; animation: fade4 8s infinite; -webkit-animation: fade4 8s infinite; } @keyframes fade { 0% { opacity: 1 } 25% { opacity: 0 } 50% { opacity: 0 } 75% { opacity: 0 } 100% { opacity: 1 } } @keyframes fade2 { 0% { opacity: 0 } 25% { opacity: 1 } 50% { opacity: 0 } 75% { opacity: 0 } 100% { opacity: 0 } } @keyframes fade3 { 0% { opacity: 0 } 25% { opacity: 0 } 50% { opacity: 1 } 75% { opacity: 0 } 100% { opacity: 0 } } @keyframes fade4 { 0% { opacity: 0 } 25% { opacity: 0 } 50% { opacity: 0 } 75% { opacity: 1 } 100% { opacity: 0 } } 
 <div class='slider'> <div class='slide1'></div> <div class='slide2'></div> <div class='slide3'></div> <div class='slide4'></div> </div> 

As suggested by @ILoveCSS this code can be shortened to just the one keyframe animation by adding a third property to the animation property. 正如@ILoveCSS所建议的那样,通过向animation属性添加第三个属性,可以将此代码缩短为一个关键帧动画。 This value is the animation-delay property and will stall the animation by the specified time: 此值是animation-delay属性,将按指定时间停止动画:

 .slider { max-width: 300px; height: 200px; margin: 20px auto; position: relative; } .slide1,.slide2,.slide3,.slide4,.slide5 { position: absolute; width: 100%; height: 100%; } .slide1 { background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371566801_p17tbs0rrjqdt1u4dnk94fe4b63.jpg)no-repeat center; background-size: cover; animation:fade 8s infinite; -webkit-animation:fade 8s infinite; } .slide2 { background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371565525_p17tbqpu0d69c21hetd77dh483.jpeg)no-repeat center; background-size: cover; animation:fade 8s infinite 2.6s; -webkit-animation:fade 8s infinite 2.6s; } .slide3 { background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371564896_p17tbq6n86jdo3ishhta3fv1i3.jpg)no-repeat center; background-size: cover; animation:fade 8s infinite 5.3s; -webkit-animation:fade 8s infinite 5.3s; } @keyframes fade { 0% {opacity:1} 33.333% { opacity: 0} 66.666% { opacity: 0} 100% { opacity: 1} } 
 <div class='slider'> <div class='slide3'></div> <div class='slide2'></div> <div class='slide1'></div> </div> 

I think what he is trying is: 我认为他在尝试的是:

@keyframes fade
{
  0%   {opacity:1}
  33.333% { opacity: 0}
  66.666% { opacity: 0}
  100% { opacity: 1}
}

in this case slide1 is getting visible at the start of the animation and stops when it reached 33.333% and 66.666% and starts to visible at the end of the animation which is 100%. 在这种情况下,slide1在动画开始时变得可见,并在达到33.333%和66.666%时停止,并在动画结束时开始显示为100%。

@keyframes fade2
{
  0%   {opacity:0}
  33.333% { opacity: 1}
  66.666% { opacity: 0 }
  100% { opacity: 0}
}

in the second slide2 at the start of the animation is not showing because slide1 is in the playing of it's animation and when slide1 reaches 33.333% started to stop and slide2 will start to fadeIn. 在动画开始时的第二张幻灯片2中没有显示,因为slide1正在播放它的动画,当slide1达到33.333%时开始停止并且slide2将开始淡入。

@keyframes fade3
{
  0%   {opacity:0}
  33.333% { opacity: 0}
  66.666% { opacity: 1}
  100% { opacity: 0}
}

in the third slide3 the animation start to fadeIn at 66.666% because at this percent slide2 is in fadeOut state and it continue like this infinitely... 在第三张幻灯片3中,动画开始淡入66.666%,因为在这个百分比中,slide2处于fadeOut状态并且它继续像这样无限...

Hope you have an idea. 希望你有个主意。

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

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