使用关键帧创建循环动画

时间:2016-12-23 07:40:20

标签: html css

我正在开发一个仅限CSS的滑块。但是我没有太多使用关键帧的经验。

我找到了这支笔;有人可以向我解释关键帧如何确保动画以循环方式运行而不是同时运行(所有幻灯片都会消失并重新出现在一起)?

Realm

.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>

2 个答案:

答案 0 :(得分:2)

这是因为每张幻灯片都使用不同的关键帧,即slide1使用fadeslide2使用fade2slide3使用fade3 }。这些关键帧都持续8秒,但显示幻灯片的帧不同:

  • slide1显示为0%(0秒)
  • slide2显示为33.333%(约2.6秒)
  • slide3显示为66.666%(约5.3秒)

当您有三张幻灯片时,这种特殊方法会起作用,但如果您有不同的数量,则需要进行调整。例如,对于四个,您需要在关键帧中添加额外的步骤:

.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>

正如@ILoveCSS所建议的那样,通过向animation属性添加第三个属性,可以将此代码缩短为仅一个关键帧动画。此值为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>

答案 1 :(得分:0)

我认为他在尝试的是:

innodb_log_file_size

在这种情况下,slide1在动画开始时可见,并在达到33.333%和66.666%时停止,并在动画结束时开始显示为100%。

@keyframes fade
{
  0%   {opacity:1}
  33.333% { opacity: 0}
  66.666% { opacity: 0}
  100% { opacity: 1}
}
在动画开始时第二张幻灯片2中的

没有显示,因为slide1正在播放它的动画,当slide1达到33.333%时开始停止并且slide2将开始淡入。

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

在第三张幻灯片3中,动画开始淡入66.666%,因为在这个百分比中,slide2处于fadeOut状态,并且它会像这样无限地继续......

希望你有个主意。