Is there anyway to animate an ellipsis with CSS animations?

前端 未结 8 1055
情话喂你
情话喂你 2021-01-29 22:58

I\'m trying to have an ellipsis animate, and was wondering if it was possible with CSS animations...

So it might be like

Loading...
Loading..
Loading.
Lo         


        
相关标签:
8条回答
  • 2021-01-29 23:34

    Here is my solution with pure css https://jsfiddle.net/pduc6jx5/1/ explained: https://medium.com/@lastseeds/create-text-ellipsis-animation-with-pure-css-7f61acee69cc

    scss

    
    
    .dot1 {
     animation: visibility 3s linear infinite;
    }
    
    @keyframes visibility {
     0% {
     opacity: 1;
     }
     65% {
     opacity: 1;
     }
     66% {
     opacity: 0;
     }
     100% {
     opacity: 0;
     }
    }
    
    .dot2 {
     animation: visibility2 3s linear infinite;
    }
    
    @keyframes visibility2 {
     0% {
      opacity: 0;
     }
     21% {
      opacity: 0;
     }
     22% {
      opacity: 1;
     }
     65% {
      opacity: 1;
     }
     66% {
      opacity: 0;
     }
     100% {
      opacity: 0;
     }
    }
    
    .dot3 {
     animation: visibility3 3s linear infinite;
    }
    
    @keyframes visibility3 {
     0% {
      opacity: 0;
     }
     43% {
      opacity: 0;
     }
     44% {
      opacity: 1;
     }
     65% {
      opacity: 1;
     }
     66% {
      opacity: 0;
     }
     100% {
      opacity: 0;
     }
    }
    
    

    html

    Loading <span class="dot dot1">.</span><span class="dot dot2">.</span><span class="dot dot3">.</span>
    
    0 讨论(0)
  • 2021-01-29 23:35

    How about a slightly modified version of @xec's answer: http://codepen.io/thetallweeks/pen/yybGra

    HTML

    A single class added to the element:

    <div class="loading">Loading</div>
    

    CSS

    Animation that uses steps. See MDN docs

    .loading:after {
      overflow: hidden;
      display: inline-block;
      vertical-align: bottom;
      -webkit-animation: ellipsis steps(4,end) 900ms infinite;      
      animation: ellipsis steps(4,end) 900ms infinite;
      content: "\2026"; /* ascii code for the ellipsis character */
      width: 0px;
    }
    
    @keyframes ellipsis {
      to {
        width: 20px;    
      }
    }
    
    @-webkit-keyframes ellipsis {
      to {
        width: 20px;    
      }
    }
    

    @xec's answer has more of a slide-in effect on the dots, while this allows the dots to appear instantly.

    0 讨论(0)
提交回复
热议问题