0
我正在写一个网站,我想通过计时器上的各种图像来设置动画效果。我有一个数组列出每个图像(作为一个数字,用于构建filename
)。动画应该通过这些数字,然后改变图像。此代码无法正常工作,我们将不胜感激。用javascript定时更改图片的问题
我正在写一个网站,我想通过计时器上的各种图像来设置动画效果。我有一个数组列出每个图像(作为一个数字,用于构建filename
)。动画应该通过这些数字,然后改变图像。此代码无法正常工作,我们将不胜感激。用javascript定时更改图片的问题
<img id="myImg" src=" http://old.ycombinator.com/images/yc500.gif" width ='100' height='100'/>
<script>
var myImages = [1, 2, 3, 5]
var img_index = 0;
var timer;
var imgId = "myImg";
// Bind the image onload event like this:
document.getElementById(imgId).onload = animate();
// Start animation
function animate() {
me = document.getElementById(imgId);
me.src = "coolimg." + myImages[img_index] + ".jpg"
img_index++;
if (img_index == myImages.length){
img_index = 0;
}
timer = setTimeout(animate, 1000);
}
</script>
我已经改善了问题的语法,并添加高亮的代码。没有代码更改,因为代码是问题的关键。 – coppro