Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I loop a css animation with multiple keyframe definitions?

Tags:

The Issue

I have two css keyframe animations which I am running on a single element:

.fade-bg {
  animation-name: fade-bg-1, fade-bg-2;
  animation-delay: 0, 6s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

The animations are defined as such:

@keyframes fade-bg-1 {

  from {
      opacity: 0;
      background-image: url(image-1.jpg);
  }

  50% {
      opacity: 1;
      background-image: url(image-1.jpg);
  }

  to {
      opacity: 0;
      background-image: url(image-1.jpg);
  }

}

@keyframes fade-bg-2 { /* Same as fade-bg-1 only with image-2.jpg  */ }

The above works but when it gets to the second animation, it keeps repeating only that animation and does not loop back to fade-bg-1.

I've tried many different combinations of animation-direction but to no avail.

The Question

How do I make it so that the animation returns to fade-bg-1 and repeats itself?

The Example

EXAMPLE

like image 378
hitautodestruct Avatar asked Aug 19 '14 13:08

hitautodestruct


People also ask

Can you have multiple keyframes CSS?

Property Values Percentage of the animation duration. Note: You can have many keyframes-selectors in one animation.

Can you have multiple key frames in an animation property?

You can animate multiple properties inside a keyframe rule and use multiple keyframes to specify an element's property values at specific points in time. For example, suppose you have an element that you want to animate from one position to another, horizontally.

How do I create an animation using @keyframes?

More "Try it Yourself" examples below. The @keyframes rule specifies the animation code. The animation is created by gradually changing from one set of CSS styles to another. During the animation, you can change the set of CSS styles many times.

What is @keyframes in CSS?

Definition and Usage. The @keyframes rule specifies the animation code. The animation is created by gradually changing from one set of CSS styles to another. During the animation, you can change the set of CSS styles many times.

How do you use CSS animation?

An animation lets an element gradually change from one style to another. You can change as many CSS properties you want, as many times you want. To use CSS animation, you must first specify some keyframes for the animation. Keyframes hold what styles the element will have at certain times.

What is a keyframes rule?

Definition and Usage The @keyframes rule specifies the animation code. The animation is created by gradually changing from one set of CSS styles to another. During the animation, you can change the set of CSS styles many times.


2 Answers

Without javascript I don't think you can. However you can achieve the same effect using a single keyframe animation.

.fade-bg {
  animation-name: fade-bg;
  animation-delay: 0;
  animation-iteration-count: infinite;
  animation-direction: forward;
}


@keyframes fade-bg {
    0% {
        opacity: 0;
        background-image: url('image-1.jpg');
    }

    25% {
        opacity: 1;
        background-image: url('image-1.jpg');
    }

    50% {
        opacity: 0;
        background-image: url('image-1.jpg');
    }

    51% {
        opacity: 0;
        background-image: url('image-2.jpg');
    }

    75% {
        opacity: 1;
        background-image: url('image-2.jpg');
    }

    100% {
        opacity: 0;
        background-image: url('image-2.jpg');
    }
}

EXAMPLE

like image 89
user3942918 Avatar answered Sep 18 '22 12:09

user3942918


I'm not sure this is possible with just css, but if you set up a setInterval method in JS cleverly, you could probably simulate the same thing by splitting the class into two.

var index = 1;

function switchBackground() {
   if (index == 1) {
      //this switches to  the first background
      var div = document.getElementById("yourDiv");
      div.className = "fade-bg-1";
      index = 0;
   }
   else {
      //this switches to  the second background
      var div = document.getElementById("yourDiv");
      div.className = "fade-bg-2";
      index = 1;
   }
}

setInterval(switchBackground(), 6000);

With .fade-bg-1 and .fade-bg-2 being the two animation classes.

Here's a jsfiddle if you want to play with it.

like image 42
Gavin Markee Avatar answered Sep 17 '22 12:09

Gavin Markee