5

Im trying to create a simple loader animation that draws a line back and forth but currently is moving only in one direction. As soon as it reaches the middle of the animation it does not animate in the oposite direction.

This is my css

@keyframes loader-animation {
     0% {
          width: 0%;
     }

     49% {
         width: 100%;
     }

     50% {
         left: 100%;
     }

     100% {
         left: 0%;
         width: 100%
     }
 }

 .loader {
     height: 5px;
     width: 100%;
 }

 .loader .bar {
     position: relative;
     height: 5px;
     background-color: dodgerblue;
     animation-name: loader-animation;
     animation-duration: 3s;
     animation-iteration-count: infinite;
     animation-timing-function: ease-in-out;
 } 

And my html

<div class="loader">
    <div class="bar"></div>
</div>

And a jsfiddle with the code

Can someone tell me what I'm doing wrong?

devconcept
  • 3,665
  • 1
  • 26
  • 40

2 Answers2

15

It is because you have a heavy break between 49% and 50%.

 49% {
     width: 100%;
 }

 50% {
     left: 100%;
 }

Adding the left to the 49%, and adjusting a few properties of width, left, etc. gives you an awesome pulsating effect:

@keyframes loader-animation {
    0% {
        width: 0%;
    }
    49% {
        width: 100%;
        left: 0%
    }
    50% {
        left: 100%;
    }
    100% {
        left: 0%;
        width: 100%
    }
}

Snippet

body {margin: 0; padding: 0;}
@keyframes loader-animation {
  0% {
    width: 0%;
  }
  49% {
    width: 100%;
    left: 0%
  }
  50% {
    left: 100%;
    width: 0;
  }
  100% {
    left: 0%;
    width: 100%
  }
}
.loader {
  height: 5px;
  width: 100%;
}
.loader .bar {
  position: absolute;
  height: 5px;
  background-color: dodgerblue;
  animation-name: loader-animation;
  animation-duration: 3s;
  animation-iteration-count: infinite;
  animation-timing-function: ease-in-out;
}
<div class="loader">
  <div class="bar"></div>
</div>

Fiddle: http://jsfiddle.net/praveenscience/06w7zwwm/

If you need a pulsating effect, you need to use two extremes:

@keyframes loader-animation {
    0% {
        left: -100%;
    }
    49% {
        left: 100%;
    }
    50% {
        left: 100%;
    }
    100% {
        left: -100%;
    }
}

Snippet

body {margin: 0; padding: 0; overflow: hidden;}
@keyframes loader-animation {
  0% {
    left: -100%;
  }
  49% {
    left: 100%;
  }
  50% {
    left: 100%;
  }
  100% {
    left: -100%;
  }
}
.loader {
  height: 5px;
  width: 100%;
}
.loader .bar {
  width: 100%;
  position: absolute;
  height: 5px;
  background-color: dodgerblue;
  animation-name: loader-animation;
  animation-duration: 3s;
  animation-iteration-count: infinite;
  animation-timing-function: ease-in-out;
}
<div class="loader">
  <div class="bar"></div>
</div>
Anand Gupta
  • 5,640
  • 3
  • 27
  • 37
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
5

I have slightly changed your code, managed to make it work. Here's what I've changed:

 @keyframes loader-animation {
     0% {
         left: -100%;
     }
     49% {
         left: 100%;
     }
     50% {
         left: 100%;
     }
     100% {
         left: -100%;
     }
 }

Added overflow: hidden; to .loader

Added width: 100%; to .loader .bar

http://jsfiddle.net/wbyzy9jL/5/

Ed T.
  • 325
  • 2
  • 12