3

这段代码提供了一个点效果,就像一个加载栏(“Loading.,Loading..,Loading...),但问题是,只有一个跨度 ID 有效,第二个无效,我不知道为什么......请帮帮我

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function showProgressDots(numberOfDots) {

    var progress = document.getElementById('progressDots');

    switch(numberOfDots) {
        case 1:
            progress.innerHTML = '.';
            timerHandle = setTimeout('showProgressDots(2)',200);
            break;
        case 2:
            progress.innerHTML = '..';
            timerHandle = setTimeout('showProgressDots(3)',200);
            break;
        case 3:
            progress.innerHTML = '...';
            timerHandle = setTimeout('showProgressDots(1)',200);
            break;
    }
}
window.setTimeout('showProgressDots(1)',100);
//-->
</SCRIPT>

Loading<span id="progressDots" style="position:absolute;"></span>
SecondLoading<span id="progressDots" style="position:absolute;"></span>
4

4 回答 4

6

如果您需要,这是简短的解决方案。

HTML:

Loading<span id="progressDots1"></span>
SecondLoading<span id="progressDots2"></span>

JavaScript:

function loading(id) {
    var el = document.getElementById(id),
        i = 0,
        dots = "...";

    setInterval(function() {
        el.innerHTML = dots.substring(0, ++i);
        if (i % 3 == 0) i = 0;
    }, 500);
}

loading("progressDots1");
loading("progressDots2");​

演示:http: //jsfiddle.net/dzFL3/

于 2012-05-25T16:24:52.040 回答
3

编辑 1:编写了一个可自定义的加载器,以便您可以注册/取消注册加载器。见下文,

编辑 2:正如 Vision 指出的那样

只是出于兴趣......我想,创建多个计时器并将它们存储在数组中有什么意义?如果您等待一段时间,单独的计时器将不同步。这个怎么样:jsfiddle.net/rMpK9/5

改进的代码:(来自 Vision 的DEMO和使用getElementsByTagName

var timer = null,
    dotLimit = 3,
    elements = [];

function registerProgressDots(progress) {
    for (var i = 0; i < progress.length; i++) {
        elements.push(progress[i]);
    }

    timer = setInterval(function() {
        for (var i = 0; i < elements.length; i++) {
            with(elements[i]) {
                innerHTML = innerHTML.length == dotLimit ? '' : innerHTML + '.';
            }
        }
    }, 200);
}

function unRegisterProgressDots(index, clearDots) {
    if (typeof index == 'undefined' || index == null) {
        clearInterval(timer);
    } else {
        elements.splice(index, 1);
        if (elements.length == 0) {
            clearInterval(timer);
        }
    }

    if (clearDots) {
        var progress = document.getElementsByClassName('progressDots');
        for (var i = 0; i < progress.length; i++) {
            progress[i].innerHTML = '';
        }
    }
}

window.setTimeout(function() {
    var spanTags = document.getElementsByTagName('span');

    var progress = [];
    for (var i = 0; i < spanTags.length; i++) {
        if (spanTags[i].className.indexOf('progressDots') >= 0) {
            progress.push(spanTags[i]);
        }
    }

    registerProgressDots(progress);
}, 100);

window.setTimeout(function() {
    unRegisterProgressDots(null, true);
}, 10000); //stop loading text after 10 seconds

最终演示

于 2012-05-25T16:14:35.943 回答
1

这是我的建议:

Loading<span id="progressDots" style="position:absolute;"></span> //element before script

<script type="text/javascript">
var i='', dots=20;

showProgressDots();    
function showProgressDots() {
    var progress = document.getElementById('progressDots');
        progress.innerHTML = i;
    i+='.';
    if (i.length<dots) setTimeout(showProgressDots, 200);
}
</script>

小提琴

于 2012-05-25T16:46:19.193 回答
1

只是发布一个我为此制作的小插件(为了它的乐趣..)。

(function($){
    $.fn['progress'] = function(_options){
        var options = {
            symbol: '.',
            delay: 200,
            length: 3,
            duration: 0
        };
        if (typeof _options === 'object'){
            $.extend(options, _options);
        } else if (_options === 'clear'){
            return this.each(function(){
                clear.apply(this);
            });
        }
        
        function display(){
            var self = $(this),
                txt = self.text() + options.symbol;
            if (txt.length  > options.length){
                txt = '';
            }
            self.text( txt );
        }
        
        function clear(){
            var self = $(this),
                timer = self.data('progressTimer');
            
            clearInterval(timer);
            self.text('');
            self.removeData('progressTimer');
        }
        
        return this.each(function(){
            var self = $(this),
                that = this,
                timer = null;
            
            timer = setInterval(function(){
                display.apply(that);
            }, options.delay);
            
            self.data('progressTimer', timer);
            
            if (options.duration){
                setTimeout(function(){
                        clear.apply(that);
                    }, options.duration);
            }
        });
    }
                         
})(jQuery);

你用它

// to set it
$('some-selector').progress({/*options*/});

// to stop it
$('some-selector').progress('clear');

有可用options的存在

  • symbol每次迭代要添加的字符(默认为.
  • length重新开始之前要显示的最大符号数(默认为 3
  • delay添加每个额外符号所需的时间(以毫秒为单位)(默认为 200
  • duration清除插件前的总持续时间(以毫秒为单位)(默认为 0,表示不自动清除

示例指向 jsfiddle

$('some-selector').progress({
  symbol: '*',
  length: 10,
  delay: 100,
  duration: 5000 
});

更新评论

要在特定时间后自动清除它,只需在您的代码中添加超时..

所以

var progressElements = $('some-selector').progress({/*options*/}); // set the progress
setTimeout(function(){
   progressElements.progress('clear');
 }, 1000); // 1000ms = 1 second

更新第二条评论

更改了上面的插件代码以允许duration参数。

如果指定,它声明插件将自动清除的时间。

在http://jsfiddle.net/gaby/gh5CD/3/演示第二个加载器将在 2 秒后清除

于 2012-05-26T14:18:46.990 回答