9

I have a very simple animation setup, to show a loading three dots. I got a bunch of them from around and picked the simplest looking one. The problem I have with it, is that it starts from 0 like it's told to. I need it to start from the end.

CSS:

.loading {
  font-size: 30px;
}

.loading:after {
  overflow: hidden;
  display: inline-block;
  vertical-align: bottom;   
  /* animation: ellipsis-dot steps(40, start) 9000ms infinite; */
  animation: ellipsis-dot 1s infinite;
  animation-fill-mode: fowards;
  content: "\2026"; /* ascii code for the ellipsis character */
  width: 0em;
}

@keyframes ellipsis {
  to {    width: 1.25em;  }
}

Here's a fiddle.

I have these showing in a table with 100s of them showing together. Which all start from completely empty. I need them to start from 3 dots. Then go to 0 then do what it's doing right now.

Note: the duration 9000 is actually 900. It's slowed down to emphasize the start of the animation after I run the fiddle.

Web_Designer
  • 72,308
  • 93
  • 206
  • 262
saGii
  • 447
  • 5
  • 14
  • Mistakes were saved. Here's the [updated version](https://jsfiddle.net/hassan_rehman/03skft9w/8/). – saGii Apr 27 '17 at 16:13

3 Answers3

18

.loading {
  font-size: 30px;
}

.loading:after {
  content: "...";
  overflow: hidden;
  display: inline-block;
  vertical-align: bottom;
  animation: ellipsis-dot 1s infinite .3s;
  animation-fill-mode: forwards;
  width: 1.25em;
}

@keyframes ellipsis-dot {
  25% {
    content: "";
  }
  50% {
    content: ".";
  }
  75% {
    content: "..";
  }
  100% {
    content: "...";
  }
}
<div class="loading">Loading</div>
danronmoon
  • 3,814
  • 5
  • 34
  • 56
Ismail Farooq
  • 6,309
  • 1
  • 27
  • 47
  • 1
    I would add a delay to the animation to be a complete answer: `ellipsis-dot 2s infinite .3s;` – m.spyratos Apr 27 '17 at 13:59
  • I was using 40 steps to kind of give it a fluid look, but i guess that's not possible with the 4step approach now is it. The underline problem is solved, so it is the correct answer. – saGii Apr 27 '17 at 16:19
  • 1
    I was waiting way longer for the animation to finish loading than I care to admit... – Per Alexandersson Apr 27 '17 at 17:57
5

.loading {
  font-size: 30px;
}

.loading:after {
  content: "\2026";
  overflow: hidden;
  display: inline-block;
  vertical-align: bottom;   
  animation: ellipsis-dot 1s infinite;
  animation-fill-mode: fowards;
  width: 1.25em;
}

@keyframes ellipsis-dot {
  50% {    
    width: 0em;
  }
  100% {
    width: 1.25em;
  }
}
<div class="loading">Loading</div>
Nick De Jaeger
  • 1,247
  • 10
  • 13
1

I'm seeing some common problems in your CSS, and I'll point them here to be more specific:

  • Your animation-fill-mode rule provides a invalid value. You need to correct it to forwards instead of "fowards".
  • The animation name differs from the animation name stated on your @keyframes rule. You'll need to correct that as well by changing one of those.
  • Suggestion: In order to maintain complete track of your animation, I suggest you to define the beginning point as well. Specifying both from and to in your @keyframes rule will save you some time, should you need to change it later.

Reference: Animation - CSS at MDN

That aside, you can apply animation-direction: reverse to your element's CSS. It will reverse the defined animation, and make it run backwards.

.loading:after {
  overflow: hidden;
  display: inline-block;
  vertical-align: bottom;   
  animation: ellipsis 1s infinite; /* Your previous rule */
  animation: ellipsis 1s infinite reverse;  /* You can reverse it like this */
  animation-direction: reverse; /* Or like this */
  content: "\2026";
  width: 0em;
}

I've updated your JSFiddle using alternate-reverse, which feels cool.

mluke
  • 99
  • 4
  • 1
    Thanks for notifying those mistakes. I was playing around to solve the initial problem. Didn't realise it was auto saving it as base. [Corrected](https://jsfiddle.net/hassan_rehman/03skft9w/8/). However, your answer is not the solution for me. The reverse animation thing looks rather odd when applied on a large html table, so i'm going to pass. Thank you for your response. – saGii Apr 27 '17 at 16:08