CSS 淡入淡出动画

Abd*_*ref 2 html css cross-fade

我使用 CSS 创建了一个交叉淡入淡出,但我在时间方面遇到了困难。我希望每个图像之间延迟 4 秒或 4 秒,但它不起作用。

#cf {
  position:absolute;
  margin:0 auto;
    width: 100%;
    height: 100%;
    top: 0;
}

#cf img {
  position:absolute;
  left:0;
  -webkit-transition: opacity 1s ease-in-out;
  -moz-transition: opacity 1s ease-in-out;
  -o-transition: opacity 1s ease-in-out;
  transition: opacity 1s ease-in-out;
    z-index: -1;
}

@-webkit-keyframes cf4FadeInOut {
 0% {
  opacity:1;
}
15% {
opacity:1;
}
55% {
opacity:0;
}
100% {
opacity:0;
}
}

@-moz-keyframes cf4FadeInOut {
 0% {
  opacity:1;
}
15% {
opacity:1;
}
55% {
opacity:0;
}
100% {
opacity:0;
}
}

@-o-keyframes cf4FadeInOut {
 0% {
  opacity:1;
}
15% {
opacity:1;
}
55% {
opacity:0;
}
100% {
opacity:0;
}
}

@keyframes cf3FadeInOut {
  0% {
  opacity:1;
}
15% {
opacity:1;
}
55% {
opacity:0;
}
100% {
opacity:0;
}
}

#cf img {
  animation-name: cf3FadeInOut;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-duration: 4s;
animation-direction: alternate;
}

#cf img:nth-of-type(1) { /* Wakame */
  -webkit-animation-delay: 24s;
  -moz-animation-delay: 24s;
  -o-animation-delay: 24s;
  animation-delay: 24s;
}
#cf img:nth-of-type(2) { /*Meraki */
  -webkit-animation-delay: 20s;
  -moz-animation-delay: 20s;
  -o-animation-delay: 20s;
  animation-delay: 20s;
}
#cf img:nth-of-type(3) { /* Trabzoni */
  -webkit-animation-delay: 16s;
  -moz-animation-delay: 16s;
  -o-animation-delay: 16s;
  animation-delay: 16s;
}
#cf img:nth-of-type(4) { /* SPS */
  -webkit-animation-delay: 12s;
  -moz-animation-delay: 12s;
  -o-animation-delay: 12s;
  animation-delay: 12s;
}

#cf img:nth-of-type(5) { /* Balad */
  -webkit-animation-delay: 8s;
  -moz-animation-delay: 8s;
  -o-animation-delay: 8s;
  animation-delay: 8s;
}

#cf img:nth-of-type(6) { /* Wesal */
  -webkit-animation-delay: 4s;
  -moz-animation-delay: 4s;
  -o-animation-delay: 4s;
  animation-delay: 4s;
}
Run Code Online (Sandbox Code Playgroud)
<div id="cf">
   <img class="img-responsive" src="http://ddragon.leagueoflegends.com/cdn/img/champion/splash/Ezreal_0.jpg" />
   <img class="img-responsive" src="http://vignette2.wikia.nocookie.net/leagueoflegends/images/6/6b/Ezreal_NottinghamSkin.jpg/revision/latest?cb=20160518164441" />
   <img class="img-responsive" src="https://s-media-cache-ak0.pinimg.com/originals/a3/99/59/a399593448b739ee3cb164f74f22d89a.jpg" />
   <img class="img-responsive" src="https://i.ytimg.com/vi/nOmafJzhUrk/maxresdefault.jpg" />
   <img class="img-responsive" src="http://art-of-lol.com/wp-content/uploads/2015/12/ezreal_by_cglas-d8smd2g.jpg" />
   <img class="img-responsive" src="http://pre14.deviantart.net/81ae/th/pre/i/2015/345/5/c/ezreal_by_tanzenkat-d9jrcvc.jpg" />
   </div>
Run Code Online (Sandbox Code Playgroud)

此外,除了时间之外,当图像交叉淡入淡出时,在第二张图像之后,它会不断回到第一张图像,而其余图像则不会显示或非常快速地显示和淡入淡出。

Abh*_*lks 6

我希望每个图像之间延迟 4 秒或 4 秒,但它不起作用

为了获得正确的时间,您需要记住一些计算。

  1. 确定每个图像应该花多长时间淡入。假设您希望图像从 0 不透明度到 1 的淡入时间为1 秒。
  2. 确定每个图像应保持可见的时间。假设您希望图像保持 1 不透明度4 秒。
  3. 确定下一个图像需要多长时间才能开始。这将成为两个图像之间的间隔。这应该比你在第 2 步得到的要大。你有 1s 显示,4s 保持显示。所以这将是至少5 秒。以增量方式将其应用于每个图像的animation-delay.
  4. 将此数字乘以您的图像总数。这将成为总计animation-duration。在这种情况下,您在步骤 #3 中计算 5s,并且您有 6 张图像。所以,你animation-duration会5 x 6 = 30即30秒。
  5. 除以100%您在第 4 步中得到的数字。这将成为keyframes. 在这种情况下,您有 30 秒的总动画持续时间。所以,你的关键帧将在步骤100% / 30 = 3.33,即3.33%,每个关键帧的。这每一帧将代表 1 秒。
  6. 在这些帧上推断您的步骤 #1 和 #2 值。即因为3.33%您必须opacity: 1在 1 秒后显示图像。然后对于每个6.66%, 9.99%,13.33%它将保持为opacity:1。即从 0 开始 4 秒,图像将保持可见。然后在帧上将16.65不透明度恢复为 0。即在第 5 秒图像将淡出。然后应保持隐藏直到 100% 即直到所有剩余的 25 秒。

把这一切放在一起,你得到:

从第 3 步开始:

#cf img:nth-child(1) { animation-delay: 0s; }
#cf img:nth-child(2) { animation-delay: 5s; }
#cf img:nth-child(3) { animation-delay: 10s; }
#cf img:nth-child(4) { animation-delay: 15s; }
#cf img:nth-child(5) { animation-delay: 20s; }
#cf img:nth-child(6) { animation-delay: 25s; }
Run Code Online (Sandbox Code Playgroud)

从第 4 步开始:

#cf img { animation: fader 30s linear infinite; }
Run Code Online (Sandbox Code Playgroud)

并且,从步骤 #5 和 #6(从步骤 #1 和 #2 推断):

@keyframes fader {
    0% { opacity: 0; } 
    03.33% { opacity: 1; }
    06.66% { opacity: 1; }
    09.99% { opacity: 1; }
    13.33% { opacity: 1; }
    16.65% { opacity: 0; }
    100% { opacity: 0; }
}
Run Code Online (Sandbox Code Playgroud)

就是这样。这是完整的片段:

片段:

#cf img:nth-child(1) { animation-delay: 0s; }
#cf img:nth-child(2) { animation-delay: 5s; }
#cf img:nth-child(3) { animation-delay: 10s; }
#cf img:nth-child(4) { animation-delay: 15s; }
#cf img:nth-child(5) { animation-delay: 20s; }
#cf img:nth-child(6) { animation-delay: 25s; }
Run Code Online (Sandbox Code Playgroud)
#cf img { animation: fader 30s linear infinite; }
Run Code Online (Sandbox Code Playgroud)