Cross-Fade between images with CSS in loop

后端 未结 2 1328
盖世英雄少女心
盖世英雄少女心 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;
    }
    <div class="fadein">
        <img id="f3" src="http://i.imgur.com/R7A9JXc.png">
        <img id="f2" src="http://i.imgur.com/D5yaJeW.png">
        <img id="f1" src="http://i.imgur.com/EUqZ1Er.png">
    </div>

    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.

    0 讨论(0)
  • 2020-12-03 08:17

    I've made a dynamic solution with SASS. It's possible to configure:

    • Total animation time
    • Amount of items
    • Transition speed

    It automatically calculates the keyframe percentages and delays between items.

    Codepen.io demo

    // Demo styles
    .fadecycle div {
      opacity: 0;
      position: absolute;
      width: 200px;
      line-height: 200px;
      text-align: center;
    }
    
    .fadecycle div:nth-child(1) { background: lightsalmon; }
    .fadecycle div:nth-child(2) { background: lightsteelblue; }
    .fadecycle div:nth-child(3) { background: lightseagreen; }
    .fadecycle div:nth-child(4) { background: lightskyblue; }
    
    // Animation settings
    $totalTime: 8s;
    $items: 4;
    $transitionSpeed: 1.5;
    
    // Calculate transition + display time in seconds
    $transitionTime: 0s + $totalTime / ($items * $transitionSpeed * 2);
    $displayTime: (0s + $totalTime - ($transitionTime * $items)) / $items;
    
    // Set transition for each element
    @for $i from 1 through $items {
      .fadecycle div:nth-child(#{$i}) {
        // Delay is increased for each item
        // starting with an offset of -$transitionTime so the first element is displayed on load
        $delay: -$transitionTime + ($transitionTime + $displayTime) * ($i - 1);
        animation: fadeinout $totalTime linear $delay infinite;
      }
    }
    
    // Calculate percentages of the times for keyframes
    $transitionPercentage: 0% + 100 * ($transitionTime / $totalTime);
    $displayPercentage: 0% + 100 * ($displayTime / $totalTime);
    
    @keyframes fadeinout {
      0% {
        opacity: 0;
      }
      #{$transitionPercentage},
      #{$transitionPercentage + $displayPercentage} {
        opacity: 1;
      }
      #{$transitionPercentage + $displayPercentage + $transitionPercentage},
      100% {
        opacity: 0;
      }
    }
    
    
    0 讨论(0)
提交回复
热议问题