Manish Pradhan
Manish Pradhan

Reputation: 1188

Css Animation - animation delay

Update - The pen below has been updated to show the end results.

I am trying to mimic signal animation using css animation but I cant seem to grasp the idea of animation delay. If you look here

http://codepen.io/anon/pen/YwZOmK?editors=110

.real-time-animation {
  margin-top: 20px;
  position: relative;
  transform: scale(0.5) rotate(45deg);
  transform-origin: 5% 0%;
}

.real-time-animation>div {
  animation: sk-bouncedelay 3s infinite forwards;
}

.circle1 {
  animation-delay: 2s;
}

.circle2 {
  animation-delay: 1s;
}

@keyframes sk-bouncedelay {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

.circle {
  position: relative;
  width: 16em;
  height: 16em;
  border-radius: 50%;
  background: transparent;
  border: 20px solid transparent;
  border-top-color: darkblue;
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
}

.circle2 {
  top: 40px;
  width: 12em;
  height: 12em;
  left: 33px;
}

.circle3 {
  top: 80px;
  width: 8em;
  height: 8em;
  left: 66px;
}
<div class="real-time-animation">
  <div class="circle circle1"> </div>
  <div class="circle circle2"> </div>
  <div class="circle circle3"> </div>
</div>

You should be able to understand what I am trying to accomplish. I want to start from showing nothing, then after 1 sec show the first bar, then after 1 sec, show the 2nd bar and finally after another 1 sec show the 3rd bar.

Upvotes: 2

Views: 923

Answers (2)

Monday Fatigue
Monday Fatigue

Reputation: 331

As I understand your question animated opacity needs to be like this:

Progress \ Element .circle1 .circle2 .circle3
0% 0 0 0
25% 0 0 1
50% 0 1 1
75% 1 1 1
100% 0 0 0

The opacity property is clamped which means if you set negative values, it will have the same effect as setting it to 0. The same goes for values larger than 1.

Using this property, we can subtract a constant value from predefined CSS variables and use that as opacity.

.real-time-animation {
  zoom: 10;
  width: 8px;
  height: 8px;
  position: relative;
  display: inline-block;
}

.real-time-animation>.circle {
  animation: circle 4s infinite ease-in;
}

.circle1 {
  --circle: 1;
}
.circle2 {
  --circle: 2;
}
.circle3 {
  --circle: 3;
}

@keyframes circle {
  0%, 100% {
   opacity: 0;
  }
  
  25% {
    opacity: calc(var(--circle) - 2);
  }
  
  50% {
    opacity: calc(var(--circle) - 1);
  }
  
  75% {
    opacity: 1;
  }
}

.circle {
    border-radius: 50%;
    background: transparent;
    border: 1px solid transparent;
    border-top-color: darkblue;
    position: absolute;
    margin: 0;
    box-sizing: border-box;
    top: 100%;
    left: 0%;
    width: calc(16px - (var(--circle) - 1)*4px);
    height: calc(16px - (var(--circle) - 1)*4px);;
    transform: rotate(45deg) translate(-50%, -50%);
    transform-origin: 0 0;
}
<div class="real-time-animation">
  <div class="circle circle1"> </div>
  <div class="circle circle2"> </div>
  <div class="circle circle3"> </div>
</div>

Upvotes: 0

Cristian Fandi&#241;o
Cristian Fandi&#241;o

Reputation: 26

My solution:

http://codepen.io/anon/pen/JGWmJg?editors=110

.real-time-animation{
  margin-top: 20px;
  position: relative;
  transform: scale(0.5) rotate(45deg);
  transform-origin: 5% 0%;
}

.circle1, .circle2, .circle3{
  animation: 4s infinite ease-in;
  animation-delay: 1s;
}

.circle1{
  animation-name: circle1;
}

.circle2{
  animation-name: circle2;
}
.circle3{
  animation-name: circle3;
}

@keyframes circle1 {
  0%{ 
   opacity: 0;
  }
  
  25%{
    opacity: 0;
  }
  
  50%{
    opacity: 0;
  }
  75%{
    opacity: 1;
  }
  
  100% { 
    opacity: 0;
  }
}

@keyframes circle2 {
  0%{ 
   opacity: 0;
  }
  
  25%{
    opacity: 0;
  }
  
  50%{
    opacity: 1;
  }
  75% { 
    opacity: 1;
  }
  100%{
    opacity: 0;
  }
}

@keyframes circle3 {
  0%{ 
   opacity: 0;
  }
  
  25%{
    opacity: 1;
  }
  
  50%{
    opacity: 1;
  }
  75% { 
    opacity: 1;
  }
  100%{
    opacity: 0;
  }
}

.circle {
    position: relative;
    width: 16em; height: 16em;
    border-radius: 50%;
    background: transparent;
    border: 20px solid transparent;
    border-top-color: darkblue;
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0;
}

.circle2{
    top: 40px;
    width: 12em;
    height: 12em;
    left: 33px;
}

.circle3{
    top: 80px;
    width: 8em;
    height: 8em;
    left: 66px;
}

You can change the speed of the animation duration: "animation: 4s infinite ease-in;"

Upvotes: 1

Related Questions