CSS3动画延迟时间

时间:2014-01-13 14:56:51

标签: css3 animation delay

我试图让每个图像以相同的时间长度显示,但是最后一个图像(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 }
}

1 个答案:

答案 0 :(得分:1)

忽略浏览器中animation-duration的不一致性,动画的数学运算已关闭。问题是因为动画中有opacity:1多长时间。这是因为25% - 4% = 21%21% * 12 > 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 } 
} 

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