此代码提供点效果,如加载栏(“正在加载..,正在加载...,正在加载...”),但问题是,只有一个单独的跨度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>
答案 0 :(得分: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");
答案 1 :(得分: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
答案 2 :(得分: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>
答案 3 :(得分: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,表示没有自动清除)$('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秒后清除)