Cross-Fade between images with CSS in loop

后端 未结 2 1342
盖世英雄少女心
盖世英雄少女心 2020-12-03 07:37

I want to fade between images in a loop (like result here-jsfiddle.net/5M2PD) but purely through CSS, no JavaScript. I tried using key-frames but I

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 08:08

    I have taken your fiddle as a base, and made it work without script.

    updated demo

    I needed to set an id to the HTML

    .fadein img {
        position:absolute;
        top:0;
        -webkit-animation-name: fade;
        -webkit-animation-iteration-count: infinite;
        -webkit-animation-duration: 6s;
        animation-name: fade;
        animation-iteration-count: infinite;
        animation-duration: 6s;
    }
    
    @-webkit-keyframes fade {
        0% {opacity: 0;}
        20% {opacity: 1;}
        33% {opacity: 1;}
        53% {opacity: 0;}
        100% {opacity: 0;}
    }
    @keyframes fade {
        0% {opacity: 0;}
        20% {opacity: 1;}
        33% {opacity: 1;}
        53% {opacity: 0;}
        100% {opacity: 0;}
    }
    
    #f1 {
        background-color: lightblue;
    }
    #f2 {
        -webkit-animation-delay: -4s;
        background-color: yellow;
    }
    #f3 {
        -webkit-animation-delay: -2s;
        background-color: lightgreen;
    }

    I am setting the keyframes to give aprox 1/3 of the time visible, with apropiate transitions. Then I set different delays for every image, so that they alternate. If you want full browser support, you will need more vendor prefixes. I have used -webkit- and bare property so that you get the idea.

提交回复
热议问题