Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using keyframes to create cyclic animation

Tags:

html

css

I am working on a CSS only slider. However I don't have much experience with using keyframes.

I found this pen; could someone explain to me how the keyframes ensure that the animation runs in a cyclic manner rather than at the same time (where all the slides would disappear and reappear together)?

Code pen link

.slider {
  max-width: 300px;
  height: 200px;
  margin: 20px auto;
  position: relative;
}
.slide1,
.slide2,
.slide3,
.slide4,
.slide5 {
  position: absolute;
  width: 100%;
  height: 100%;
}
.slide1 {
  background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371566801_p17tbs0rrjqdt1u4dnk94fe4b63.jpg)no-repeat center;
  background-size: cover;
  animation: fade 8s infinite;
  -webkit-animation: fade 8s infinite;
}
.slide2 {
  background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371565525_p17tbqpu0d69c21hetd77dh483.jpeg)no-repeat center;
  background-size: cover;
  animation: fade2 8s infinite;
  -webkit-animation: fade2 8s infinite;
}
.slide3 {
  background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371564896_p17tbq6n86jdo3ishhta3fv1i3.jpg)no-repeat center;
  background-size: cover;
  animation: fade3 8s infinite;
  -webkit-animation: fade3 8s infinite;
}
@keyframes fade {
  0% {
    opacity: 1
  }
  33.333% {
    opacity: 0
  }
  66.666% {
    opacity: 0
  }
  100% {
    opacity: 1
  }
}
@keyframes fade2 {
  0% {
    opacity: 0
  }
  33.333% {
    opacity: 1
  }
  66.666% {
    opacity: 0
  }
  100% {
    opacity: 0
  }
}
@keyframes fade3 {
  0% {
    opacity: 0
  }
  33.333% {
    opacity: 0
  }
  66.666% {
    opacity: 1
  }
  100% {
    opacity: 0
  }
}
<div class='slider'>
  <div class='slide1'></div>
  <div class='slide2'></div>
  <div class='slide3'></div>
</div>
like image 789
Rishabh Kapoor Avatar asked Sep 29 '26 03:09

Rishabh Kapoor


1 Answers

This is due to the each slide using a different keyframe, i.e. slide1 uses fade, slide2 uses fade2 and slide3 uses fade3. These keyframes all last 8 seconds however the frame in which the slide is shown is different:

  • slide1 is shown at 0% (0 seconds)
  • slide2 is shown at 33.333% (about 2.6 seconds)
  • slide3 is shown at 66.666% (about 5.3 seconds)

This particular method will work when you have three slides but would need to be adapted if you were to have a different amount. For example with four you would need to add an extra step to the keyframe:

.slider {
  max-width: 300px;
  height: 200px;
  margin: 20px auto;
  position: relative;
}
.slide1,
.slide2,
.slide3,
.slide4,
.slide5 {
  position: absolute;
  width: 100%;
  height: 100%;
}
.slide1 {
  background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371566801_p17tbs0rrjqdt1u4dnk94fe4b63.jpg)no-repeat center;
  background-size: cover;
  animation: fade 8s infinite;
  -webkit-animation: fade 8s infinite;
}
.slide2 {
  background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371565525_p17tbqpu0d69c21hetd77dh483.jpeg)no-repeat center;
  background-size: cover;
  animation: fade2 8s infinite;
  -webkit-animation: fade2 8s infinite;
}
.slide3 {
  background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371564896_p17tbq6n86jdo3ishhta3fv1i3.jpg)no-repeat center;
  background-size: cover;
  animation: fade3 8s infinite;
  -webkit-animation: fade3 8s infinite;
}
.slide4 {
  background: red;
  background-size: cover;
  animation: fade4 8s infinite;
  -webkit-animation: fade4 8s infinite;
}
@keyframes fade {
  0% {
    opacity: 1
  }
  25% {
    opacity: 0
  }
  50% {
    opacity: 0
  }
  75% {
    opacity: 0
  }
  100% {
    opacity: 1
  }
}
@keyframes fade2 {
  0% {
    opacity: 0
  }
  25% {
    opacity: 1
  }
  50% {
    opacity: 0
  }
  75% {
    opacity: 0
  }
  100% {
    opacity: 0
  }
}
@keyframes fade3 {
  0% {
    opacity: 0
  }
  25% {
    opacity: 0
  }
  50% {
    opacity: 1
  }
  75% {
    opacity: 0
  }
  100% {
    opacity: 0
  }
}
@keyframes fade4 {
  0% {
    opacity: 0
  }
  25% {
    opacity: 0
  }
  50% {
    opacity: 0
  }
  75% {
    opacity: 1
  }
  100% {
    opacity: 0
  }
}
<div class='slider'>
  <div class='slide1'></div>
  <div class='slide2'></div>
  <div class='slide3'></div>
  <div class='slide4'></div>
</div>

As suggested by @ILoveCSS this code can be shortened to just the one keyframe animation by adding a third property to the animation property. This value is the animation-delay property and will stall the animation by the specified time:

.slider {
  max-width: 300px;
  height: 200px;
  margin: 20px auto;
  position: relative;
}
.slide1,.slide2,.slide3,.slide4,.slide5 {
  position: absolute;
  width: 100%;
  height: 100%;
}
.slide1 {
  background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371566801_p17tbs0rrjqdt1u4dnk94fe4b63.jpg)no-repeat center;
      background-size: cover;
    animation:fade 8s infinite;
-webkit-animation:fade 8s infinite;

} 
.slide2 {
  background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371565525_p17tbqpu0d69c21hetd77dh483.jpeg)no-repeat center;
      background-size: cover;
    animation:fade 8s infinite 2.6s;
-webkit-animation:fade 8s infinite 2.6s;
}
.slide3 {
    background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371564896_p17tbq6n86jdo3ishhta3fv1i3.jpg)no-repeat center;
      background-size: cover;
    animation:fade 8s infinite 5.3s;
-webkit-animation:fade 8s infinite 5.3s;
}
@keyframes fade
{
  0%   {opacity:1}
  33.333% { opacity: 0}
  66.666% { opacity: 0}
  100% { opacity: 1}
}
<div class='slider'>
  <div class='slide3'></div>
  <div class='slide2'></div>
  <div class='slide1'></div>
</div>
like image 193
Hidden Hobbes Avatar answered Oct 01 '26 17:10

Hidden Hobbes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!