我有下面的图像,我试图以3秒的间隔一张一张地显示它们,但我无法使其正常工作。它继续保持在0且不显示图像,帮助会很不错:
<img src="one.png"></img>
<img src="two.png"></img>
javascript:
window.animate = function(){
var timer = '';
var imgs = document.getElementsByTagName('img');
for (var i = 0; i < imgs.length; i++) {
var timer = setInterval(function(){
alert(i);
imgs[i].style.display = 'block';
}, 3000);
if(i == imgs.length){
clearInterval(timer);
}
}
}
最佳答案
这可能是您要寻找的:
window.animate = function(){
var imgs = document.getElementsByTagName('img');
var index = 0;
var timer = setInterval(function() {
// Hide all imgs
for (var i = 0; i < imgs.length; i++)
imgs[i].style.display = 'none';
// Display next img
imgs[index].style.display = 'block';
index++;
// Stop after all displayed.
if (index >= imgs.length)
clearInterval(timer);
}, 3000);
}
关于javascript - Javascript以间隔一一显示img,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33636834/