【问题标题】:CSS3 Animation Delay TimesCSS3 动画延迟时间
【发布时间】:2014-01-14 00:25:52
【问题描述】:

我试图让每张图片都以相同的时间显示,但是最后一张图片 (12) 似乎卡住了,然后它会切换到图片 3 而不是图片 1 以再次开始动画?

我已将动画设置为持续 4 秒,即 48/12 张图像。实在想不通为什么图片 12 会转换到图片 3 而不是 1??

HTML

<div id="container">
    <div id="crossfade">
        <img src="images/img1.jpg">
        <img src="images/img2.jpg">
        <img src="images/img3.jpg">
    <img src="images/img4.jpg">
        <img src="images/img5.jpg">
        <img src="images/img6.jpg">
    <img src="images/img7.jpg">
        <img src="images/img8.jpg">
    <img src="images/img9.jpg">
        <img src="images/img10.jpg">
        <img src="images/img11.jpg">
    <img src="images/img12.jpg">
        </div>
</div>

CSS

#fadecontainer {
    position: relative;
    margin-bottom: 390px;
}

#crossfade > img {
    position: absolute;
    top: 10px;
    left: 0px;
    color: transparent;
    border: 1px solid #000000;
    opacity: 0;
    z-index: 0;
    -webkit-backface-visibility: hidden;
    -webkit-animation: imageAnimation 48s linear infinite 0s;
    -moz-animation: imageAnimation 48s linear infinite 0s;
    -o-animation: imageAnimation 48s linear infinite 0s;
    -ms-animation: imageAnimation 48s linear infinite 0s;
    animation: imageAnimation 48s linear infinite 0s;
}

#crossfade > img:nth-child(1)  {
    -webkit-animation-delay: 0s;
    -moz-animation-delay: 0s;
    -o-animation-delay: 0s;
    -ms-animation-delay: 0s;
    animation-delay: 0s;
}

#crossfade > img:nth-child(2)  {
    -webkit-animation-delay: 4s;
    -moz-animation-delay: 4s;
    -o-animation-delay: 4s;
    -ms-animation-delay: 4s;
    animation-delay: 4s;
}

#crossfade > img:nth-child(3) {
    -webkit-animation-delay: 8s;
    -moz-animation-delay: 8s;
    -o-animation-delay: 8s;
    -ms-animation-delay: 8s;
    animation-delay: 8s;
}

#crossfade > img:nth-child(4) {
    -webkit-animation-delay: 12s;
    -moz-animation-delay: 12s;
    -o-animation-delay: 12s;
    -ms-animation-delay: 12s;
    animation-delay: 12s;
}

#crossfade > img:nth-child(5) {
    -webkit-animation-delay: 16s;
    -moz-animation-delay: 16s;
    -o-animation-delay: 16s;
    -ms-animation-delay: 16s;
    animation-delay: 16s;
}

#crossfade > img:nth-child(6) {
    -webkit-animation-delay: 20s;
    -moz-animation-delay: 20s;
    -o-animation-delay: 20s;
    -ms-animation-delay: 20s;
    animation-delay: 20s;
}
#crossfade > img:nth-child(7) {
    -webkit-animation-delay: 24s;
    -moz-animation-delay: 24s;
    -o-animation-delay: 24s;
    -ms-animation-delay: 24s;
    animation-delay: 24s;
}

#crossfade > img:nth-child(8) {
    -webkit-animation-delay: 28s;
    -moz-animation-delay: 28s;
    -o-animation-delay: 28s;
    -ms-animation-delay: 28s;
    animation-delay: 28s;
}

#crossfade > img:nth-child(9) {
    -webkit-animation-delay: 32s;
    -moz-animation-delay: 32s;
    -o-animation-delay: 32s;
    -ms-animation-delay: 32s;
    animation-delay: 32s;
}

#crossfade > img:nth-child(10) {
    -webkit-animation-delay: 36s;
    -moz-animation-delay: 36s;
    -o-animation-delay: 36s;
    -ms-animation-delay: 36s;
    animation-delay: 36s;
}

#crossfade > img:nth-child(11) {
    -webkit-animation-delay: 40s;
    -moz-animation-delay: 40s;
    -o-animation-delay: 40s;
    -ms-animation-delay: 40s;
    animation-delay: 40s;
}

#crossfade > img:nth-child(12) {
    -webkit-animation-delay: 44s;
    -moz-animation-delay: 44s;
    -o-animation-delay: 44s;
    -ms-animation-delay: 44s;
    animation-delay: 44s;
}

    @-webkit-keyframes imageAnimation {
    0% { opacity: 0;
    -webkit-animation-timing-function: ease-in; }
    4% { opacity: 1;
         -webkit-animation-timing-function: ease-out; }
    25% { opacity: 1 }
    30% { opacity: 0 }
    100% { opacity: 0 }
}

@-moz-keyframes imageAnimation {
    0% { opacity: 0;
    -moz-animation-timing-function: ease-in; }
    4% { opacity: 1;
         -moz-animation-timing-function: ease-out; }
    25% { opacity: 1 }
    30% { opacity: 0 }
    100% { opacity: 0 }
}

@-o-keyframes imageAnimation {
    0% { opacity: 0;
    -o-animation-timing-function: ease-in; }
    4% { opacity: 1;
         -o-animation-timing-function: ease-out; }
    25% { opacity: 1 }
    30% { opacity: 0 }
    100% { opacity: 0 }
}

@-ms-keyframes imageAnimation {
    0% { opacity: 0;
    -ms-animation-timing-function: ease-in; }
    4% { opacity: 1;
         -ms-animation-timing-function: ease-out; }
    25% { opacity: 1 }
    30% { opacity: 0 }
    100% { opacity: 0 }
}

@keyframes imageAnimation {
    0% { opacity: 0;
    animation-timing-function: ease-in; }
    4% { opacity: 1;
         animation-timing-function: ease-out; }
    25% { opacity: 1 }
    30% { opacity: 0 }
    100% { opacity: 0 }
}

【问题讨论】:

  • 你用的是什么浏览器?如果它是 IE 或像现代 FireFox 这样的无前缀浏览器,那么您会遇到问题,因为动画持续时间是 58s,而它应该是 48s
  • 我已经在 Safari 上测试过了
  • 所以你改变了我提到的动画持续时间的错误,问题仍然存在?
  • 是的,我注意到了 58 的两个实例。最后一张图像仍然滞后,然后它转换到图像 3。
  • 我认为问题在于您在动画中拥有opacity:1 多长时间。这是因为25% - 4% = 21%21% * 12 &gt; 100%。相反,差异应该更像100% / 12 = 8.333333%,即关键帧动画中的 4% 到 12.333%

标签: css animation delay


【解决方案1】:

忽略浏览器之间animation-duration 中的不一致,您的动画的数学是关闭的。问题在于您在动画中拥有opacity:1 多长时间。这是因为25% - 4% = 21%21% * 12 &gt; 100%。区别应该更像100% / 12 = 8.333333%

@keyframes imageAnimation { 
    0% { opacity: 0; animation-timing-function: ease-in; } 
    2% { opacity: 1; animation-timing-function: ease-out; } 
    8.333% { opacity: 1 } 
    10% { opacity: 0 } 
    100% { opacity: 0 } 
} 

当然,您必须为动画添加前缀才能获得跨浏览器性能

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    相关资源
    最近更新 更多