2013-03-13 78 views
11

我正在寻找一种显示标点加载“动画”的好方法。标点符号加载“动画”,javascript?

我要的是这样的:

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...." 

等。

我开始围绕spans中的小点,并认为我可以通过jquery遍历它们并多显示一个,多一个,然后重置为1.但代码变得非常笨拙,所以我不知道你会怎么做做这个?

+0

使用关键帧动画,与::后不同的内容元素或许.. – techfoobar 2013-03-13 10:28:59

回答

13

制作一串点的诀窍是制作一个稀疏数组,然后将所有元素与所需字符连接起来。

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

这是Live demo

+0

的作品就像一个魅力,谢谢:) – 2013-03-13 10:36:00

+0

你也可以预先缓存点字符串以避免重新创建数组ach迭代(但是然后再次性能增益可能是最小的)。 – monsur 2013-03-13 16:07:11

+0

工程处理。虽然它降低了可读性,但我并不是速记的主要粉丝。 “count%10”是什么意思? – 2014-07-24 14:56:55

1

现在确定代码如何一发不可收拾,你可以只是做:

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> 

而且作为1,你可以换说出来与周期数。

0

试试这个功能:我心中已经把一个例子这里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

这是一个非常简单的变体:http://jsfiddle.net/psycketom/FusdC/

阅读下面的意见,了解什么一切都在那里做什么。

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 

在这里你还可以获得使用easing如果你愿意的话,很容易地改变总的持续时间和一堆其他好处的情况下,你可以安心使用jQuery的优势。

0

伙计,除非你想要显示这个动画永远你需要一种方法来停止动画,或者?

甚至不要去想全局变量,这是的JavaScript和它有封闭该:)

<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

纯CSS解决方案

演示: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的唯一选择

优势:没有嵌套的标签。这意味着省略号可以作为内容 放入::after伪元素中。

演示: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

等待您的输入 ... 2014-06-22 09:43:16

3

这可以很容易:

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