2013-03-13 13 views
11

Sto cercando un buon modo per visualizzare la punteggiatura caricando "animazione".Punteggiatura caricamento "animazione", javascript?

Quello che voglio è qualcosa di simile:

This will display at second 1: "Waiting for your input." 
This will display at second 2: "Waiting for your input.." 
This will display at second 3: "Waiting for your input..." 
This will display at second 4: "Waiting for your input...." 
This will display at second 5: "Waiting for your input." 
This will display at second 6: "Waiting for your input.." 
This will display at second 7: "Waiting for your input..." 
This will display at second 8: "Waiting for your input...." 

E così via.

Ho iniziato circondando i punti in spans e ho pensato di poterli scorrere in loop con jquery e visualizzarne uno in più, uno in più, uno in più, quindi resettare su 1. Ma il codice è diventato molto goffo, quindi mi chiedo come si Fai questo?

+0

Usa key-frame, con una :: dopo elemento con contenuti diversi, forse .. – techfoobar

risposta

13

Il trucco per creare una serie di punti è creare una matrice sparsa e quindi unire tutti gli elementi con il carattere desiderato.

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

Ecco uno Live demo.

+0

funziona come un fascino , grazie :) –

+0

Puoi anche pre-memorizzare le stringhe di punti per evitare di ricreare l'array su e ach iterazione (ma di nuovo il guadagno in termini di prestazioni potrebbe essere minimo). – monsur

+0

Lavori trattati. Non sono un grande fan della stenografia anche se riduce la leggibilità. Cosa significa "contare% 10"? –

1

Ora sicuro di come il codice è sfuggita di mano, si può solo fare:

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> 

E per quanto riguarda la 1, è possibile scambiare che fuori con il numero di periodi.

0

provare questa funzione: i'v messo un esempio qui http://jsfiddle.net/XFd39/

var i=0; 
function f() { 
if(i<=4) 
$('#a').append("."); 
i++; 
if(i==4){ 
    $('#a').html(""); 
    i=0; 
} 
setTimeout(f,500); 
} 
f(); 
0

Ecco una bella semplice variante: http://jsfiddle.net/psycketom/FusdC/

Leggere i commenti qui sotto per capire cosa tutto sta facendo lì.

var span = $('.dots'); // take the element where you have the maximum count of dots 
var text = span.text(); // cahce it's text value 

// we set up a function here, so we can loop it all the time 
var loading = function() 
{ 
    $({ 
     count : 1 // we start at one dot 
    }).animate({ 
     count : text.length // together forming all of it 
    }, { 
     duration : 1000, // make the animation complete in one second 
     step : function() { 
      span.text(text.substring(0, Math.round(this.count))); // on each step, change the text accordingly to current iteration 
     }, 
     complete : function() { 
      loading(); // once complete, start all over again 
     } 
    }); 
}; 

loading(); // start it up for the first time 

Qui anche guadagnare il vantaggio di utilizzare easing se lo si desidera, facilmente cambiando durata totale e mazzo di altri benefici nel caso in cui si sta bene con jQuery.

0

Amico, a meno che tu non voglia visualizzare questa animazione per sempre avrai bisogno di un modo per fermare l'animazione, o?

Non pensate neppure di variabili globali, questo è Javascript ed è ottenuto per chiusure che :)

<p>please wait<span id="wait"></span></p> 

<input type="submit" id="start" value="start"> 
<input type="submit" id="stop" value="stop"> 

<script type="text/javascript"> 
    $(document).ready(function() { 

     var animator = function($el) { 
      var dotCount = 1; 
      var started = true; 
      return { 
       "start" : function step() { 
        dotCount = (dotCount + 1) % 10; 
        $el.text(new Array(dotCount).join('.')); 
        if (started) { 
         setTimeout(step, 100); 
        } 
       }, 
       "stop" : function() { 
        started = false; 
       } 
      } 
     }; 

     var animatedWait = animator($("#wait")); 

     $("#start").click(animatedWait.start); 
     $("#stop").click(animatedWait.stop); 
    }); 
</script> 
10

Pure CSS soluzione

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-unica alternativa

Vantaggio: Nessun tag annidati. Ciò significa che i puntini di sospensione potrebbero essere inseriti come contenuto in uno pseudo elemento ::after.

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

In attesa del tuo input ...

3

Questo può essere molto facile:

HTML

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

JQuery animazione

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

Demo

Problemi correlati