-2

我一直在努力尝试实现 TheNoble-Coder 的答案,以理解幻灯片脚本的困难。他的“如果你想要……”解决方案对于包含不同数量图像的文件夹来说,听起来像是一个非常优雅的解决方案。但是我一直无法让它工作。使用 Yaldex 的免费 JavaScript 编辑器进行调试,我得到指示行的“错误:不兼容的类型”:

var place; //For convenience this is best as a global
function imageItem(url) 
{
    var img = new Image();
    img.src = url;
    return url;
}
for(var i=0; i<5; i++)
{
    imageArray[i] = imageItem(imageDir + i + ".jpg");   <-----Incompatible 
}

我真的很感激任何帮助。

整个脚本是:

     <script language="JavaScript">
var interval = 4000;
var random_display = 0;
var imageDir = "images/";
var imageNum = - 1;
var place; //For convenience this is best as a global
function imageItem(url) 
{
    var img = new Image();
    img.src = url;
    return url;
}
for(var i=0; i<5; i++)
{
    imageArray[i] = imageItem(imageDir + i + ".jpg");
}
function randNum(x, y)
{
   var range = y - x + 1;
   return Math.floor(Math.random() * range) + x;
}
function getNextImage() 
{
    imageNum = (random_display) ? randNum(0, imageArray.length-1) : ((imageNum+1) % imageArray.length);
    return imageArray[imageNum];
}
function getPrevImage()
{
    imageNum = (imageNum-1 >= 0) ? (imageNum-1) : (imageArray.length-1);
    return imageArray[imageNum];
}
function prevImage() 
{
    var image_item = getPrevImage();
    place.src = place.alt = image_item.url;
}
function nextImage() 
{
    var image_item = getNextImage();
    place.src = place.alt = image_item.url;
}
function play() 
{
    nextImage();
    timerID = setTimeout(play, interval);
}
window.onload = function()
{
    place = document.slideImg;
    play();
};

  </script>
4

1 回答 1

0

尝试打印imageArray[i]imageItem(imageDir + i + ".jpg")查看它是否符合您的期望。我相信这是解决方案的关键...... :)

于 2012-09-08T15:21:45.527 回答