【问题标题】:CSS Animation Delay and KeyframeCSS 动画延迟和关键帧
【发布时间】:2018-04-21 23:26:26
【问题描述】:

我在 CSS Animation 上遇到动画延迟问题。我有 3 张图片,我想制作幻灯片。插图是,图像 1 到图像 2 需要 15 秒才能改变,图像 2 到图像 3 需要 15 秒才能改变,图像 3 回到图像 1 需要 30 秒,在第一个循环之后,我想让幻灯片结束图像 3 所以图像 1 到图像 2 仍为 15 秒,图像 2 到图像 3 仍为 15 秒,当图像 3 加载时无需返回图像 1。我尝试了此代码,但它给了我 15 秒的所有图像延迟。这是我的代码:

ul {
      list-style: none;
      margin: 0;
      padding: 0;
      position: relative;
    }
    
    li {
      position: absolute;
      opacity:0;
    }
    
    li {
      animation: xfade 45s  infinite;
    }
    li:nth-child(2) {
      animation-delay:15s;
    }
    li:nth-child(3) {
      animation-delay:30s;
    }
    
    @keyframes xfade{ 
      3%{opacity:1}
      33% {opacity:1;}
      36%{opacity:0}
    }
    <ul>
      <li><img width="500" height="500" src="http://lorempixel.com/500/500/sports" alt="pic1"></li>
      <li><img width="500" height="500" src="http://lorempixel.com/500/500/people" alt="pic2"></li>
      <li><img width="500" height="500" src="http://lorempixel.com/500/500/transport" alt="pic3"></li>
    </ul>

我想根据上面的插图延迟我的动画。任何人都可以帮我解决这个问题吗?之前谢谢你。

【问题讨论】:

  • 这是最好的解决方案,但为此,您必须使用 gsap 动画,因为 CSS 动画不是我们想要的。因此,为了获得最佳效果,您必须使用 gsap 动画,这很容易。这是直播网址greensock.com/gsap
  • @HardenRahul 感谢您的建议。但是,GSAP 使用了 javascript,我不能使用 javascript,因为我的客户不想使用 javascript。
  • 那么你可以使用代码笔中的这个例子这里是 URL:codepen.io/daysahead/pen/mJqBge
  • @Antonio 也检查我的解决方案。

标签: html css


【解决方案1】:

这里有一些真正值得思考的东西:)

我必须应用 2 个动画来改变不透明度:xfade-25pctxfade-50pct。两者都只播放 2 次,在动画的 25% 和 50% 后短暂淡出。还有一个额外的 show 动画,用于在 2 个动画循环后制作第三张图像,并使用必要的规则 animation-fill-mode: forwards;

不透明度的诀窍是:您必须将动画分成 4 个部分。如果需要,您可以将总动画持续时间从 60s 更改为 4 的倍数,并调整延迟。第三个动画延迟是第二个的两倍。

----#----#----#----#----#----#----#----#----#----#
1st animation      | 1st animation     |
--------------------------------------------------
15s | 2nd animation     | 2nd animation     |
--------------------------------------------------
30s      | 3rd animation     |   3rd animation   |
----#----#----#----#----#----#----#----#----#----#

欢迎提问。希望对您有所帮助。

var s = 0,
  c = 1;
/* Code for displaying timer */
window.setInterval(function() {
  s++;
  document.querySelector('DIV').innerHTML = s;
  if (s == 15 && c <= 2 || s == 30) {
    if (s == 30) {
      c = 1;
    } else {
      c++;
    }
    s = 0;
  }
}, 1000);
ul {
  list-style: none;
  margin: 0;
  padding: 0;
  position: relative;
}

li {
  position: absolute;
  opacity: 0;
}

li {
  animation: xfade-25pct 60s 2;
}

li:nth-child(2) {
  animation-delay: 15s;
}

li:nth-child(3) {
  animation-delay: 30s, 120s;
  animation-name: xfade-50pct, show;
  animation-duration: 60s, 1s;
  animation-iteration-count: 2, 1;
  animation-fill-mode: forwards;
}

@keyframes xfade-25pct {
  0% {
    opacity: 0;
  }
  2%,
  25% {
    opacity: 1;
  }
  27% {
    opacity: 0;
  }
}

@keyframes xfade-50pct {
  0% {
    opacity: 0;
  }
  2%,
  50% {
    opacity: 1;
  }
  52% {
    opacity: 0;
  }
}

@keyframes show {
  0%,
  100% {
    opacity: 1;
  }
}
<DIV></DIV>
<ul>
  <li><img width="500" height="500" src="https://www.noao.edu/image_gallery/images/d2/pelican_500.jpg" alt="pic1"></li>
  <li><img width="500" height="500" src="http://whc.unesco.org/uploads/thumbs/news_920-500-500-20151015155516.jpg" alt="pic2"></li>
  <li><img width="500" height="500" src="https://i.pinimg.com/736x/4c/17/65/4c176537aee906de294138c3bac5b8f5--merry-christmas-love-coffee-aroma.jpg" alt="pic3"></li>
</ul>

【讨论】:

    【解决方案2】:

    如果你想要像这样的特定场景的动画,我认为使用 GreenSock 会更好。

    这是我可以使用 HTML 和 CSS 获得的最接近的值,我还需要复制 &lt;li&gt; 以适应您的场景。

    ul {
          list-style: none;
          margin: 0;
          padding: 0;
          position: relative;
        }
        
        li {
          position: absolute;
          opacity: 0;
        }
        
        li:nth-child(6) {
          /*The last item always on the top, direction will goes from last to first*/
          animation: xfade 15s;
        }
        li:nth-child(5) {
          /*Put animation length double the delay (in this case delay is the actual animation length)*/
          animation: xfade 30s 15s;
        }
        li:nth-child(4) {
          animation: xfade 30s 15s;
        }
        li:nth-child(3) {
          animation: xfade 30s 15s;
        }
        li:nth-child(2) {
          animation: xfade 30s 15s;
        }
        li:nth-child(1) {
          opacity: 1;
        }
        
        @keyframes xfade{ 
          0%{opacity:0}
          33% {opacity:1;}
          100%{opacity:0}
        }
    <ul>
          <li>1<img width="500" height="500" src="http://lorempixel.com/500/500/sports" alt="pic1"></li>
          <li>2<img width="500" height="500" src="http://lorempixel.com/500/500/people" alt="pic2"></li>
          <li>3<img width="500" height="500" src="http://lorempixel.com/500/500/transport" alt="pic3"></li>
    
          <!-- Duplicate -->
          <li>4<img width="500" height="500" src="http://lorempixel.com/500/500/sports" alt="pic1"></li>
          <li>5<img width="500" height="500" src="http://lorempixel.com/500/500/people" alt="pic2"></li>
          <li>6<img width="500" height="500" src="http://lorempixel.com/500/500/transport" alt="pic3"></li>
        </ul>

    【讨论】:

    • 延迟如何?你能在 15 秒、15 秒和 30 秒内完成吗?
    • 是的,只需将nth-child(4) 更新为xfade 45s 30s 即可。我认为您最好修改 xfade 关键帧百分比以使其更平滑。
    • 来自animation: xfade 30s 15s; 有 30s 和 15s。你能解释一下 30s 和 15s 是什么意思吗?
    • 第一个数字是动画长度,第二个数字是动画延迟。实际动画不仅淡出(不透明度 1 -> 0)而且淡入淡出(不透明度 0 -> 1 -> 0)。这使得动画需要在它实际显示之前开始,这样当它显示给用户时,它已经有opacity: 1。有了这个逻辑,你的“动画长度”场景就变成了“动画延迟”。
    猜你喜欢
    • 2013-12-28
    • 2014-05-25
    • 2015-10-27
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    相关资源
    最近更新 更多