Search code examples
cssanimationdelay

CSS3 Animation Delay Times


I am trying to make each image display at equal lengths of time, however the last image (12) seems to get stuck, then it shifts to image 3 instead instead of image 1 to start the animation again?

I have set the animation to last 4 seconds which is 48/12 images. Really can't figure out why image 12 transitions to image 3 instead of 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 }
}

Solution

  • Ignoring the inconsistencies in animation-duration among browsers, the math of your animation is off. The issue is because of how long you have opacity:1 in the animation. This is because 25% - 4% = 21% and 21% * 12 > 100%. The difference should instead be more like 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 } 
    } 
    

    You have to prefix the animations to get cross-browser performance of course