Punctuation loading “animation”, javascript?

后端 未结 4 1386
甜味超标
甜味超标 2021-02-19 17:52

I\'m looking for a good way to display some punctuation loading \"animation\".

What I want is something like this:

This will display at second 1: \"Waiti         


        
相关标签:
4条回答
  • 2021-02-19 18:20

    Pure CSS solution

    Demo: jsfiddle.net/feklee/D59P9

    • HTML:

      Waiting<span class="dots"><span>.</span><span>.</span><span>.</span></span> for more.
      
    • CSS:

      @keyframes dots-1 { from { opacity: 0; } 25% { opacity: 1; } }
      @keyframes dots-2 { from { opacity: 0; } 50% { opacity: 1; } }
      @keyframes dots-3 { from { opacity: 0; } 75% { opacity: 1; } }
      @-webkit-keyframes dots-1 { from { opacity: 0; } 25% { opacity: 1; } }
      @-webkit-keyframes dots-2 { from { opacity: 0; } 50% { opacity: 1; } }
      @-webkit-keyframes dots-3 { from { opacity: 0; } 75% { opacity: 1; } }
      
      .dots span {
          animation: dots-1 1s infinite steps(1);
          -webkit-animation: dots-1 1s infinite steps(1);
      }
      
      .dots span:first-child + span {
          animation-name: dots-2;
          -webkit-animation-name: dots-2;
      }
      
      .dots span:first-child + span + span {
          animation-name: dots-3;
          -webkit-animation-name: dots-3;
      }
      

    WebKit-only alternative

    Advantage: No nested tags. This means that the ellipsis could be put as content into an ::after pseudo element.

    Demo: jsfiddle.net/feklee/vFT7W

    • HTML:

      Waiting<span>...</span> for more.
      
    • CSS:

      body {
          font-family: 'Roboto', sans-serif;
          font-size: 50px;
      }
      
      @-webkit-keyframes dots {
          0% { background-position: 0px; }
          100% { background-position: 50px; }
      }
      
      span {
          background: linear-gradient(to right, white 50%, black 50%);
          color: transparent;
          -webkit-background-clip: text;
          -webkit-animation: dots 1s infinite steps(4);
          padding-right: 40px;
          margin-right: -40px;
      }
      
    0 讨论(0)
  • 2021-02-19 18:26

    The trick to making a string of dots is to make a sparse Array and then join all the elements with the desired character.

    var count = 0;
    setInterval(function(){
        count++;
        var dots = new Array(count % 10).join('.');
        document.getElementById('loadingtext').innerHTML = "Waiting for your input." + dots;
      }, 1000);
    

    Here is a Live demo.

    0 讨论(0)
  • 2021-02-19 18:26

    This can be very easy:

    HTML

    <span class="dots"></span>
    

    JQuery

    setInterval(function() {
        var th = $('.dots');
        if(th.text().length < 5){
            th.text(th.text()+".");
        }else{
            th.text("");
        }
    }, 500);
    

    Demo

    0 讨论(0)
  • 2021-02-19 18:32

    Now sure how the code got out of hand, you could just do:

    setInterval(function () {
      var span = $("#text-loader").children("span:eq(0)");
      var ellipsis = span.html();
      ellipsis = ellipsis + ".";
      if (ellipsis.length > 5) {
        ellipsis = ".";
      }
      span.html(ellipsis);
    }, 1000);
    
    <div id="text-loader">
      This will display at second 1: "Waiting for your input<span>.</span>
    </div>
    

    And as for the 1, you can swap that out with the number of periods.

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