Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript图像转换器的问题_Javascript - Fatal编程技术网

Javascript图像转换器的问题

Javascript图像转换器的问题,javascript,Javascript,我的页面上运行了一个脚本来更改图像。我想重复6次,以便在同一页的其他地方使用,但当我重复时,它不会起作用 var delay = 2000 //set delay in miliseconds var curindex = 0 var randomimages = new Array() randomimages[0] = "hhh200.jpg" randomimages[1] = "ray200.jpg" var preload = new Array()

我的页面上运行了一个脚本来更改图像。我想重复6次,以便在同一页的其他地方使用,但当我重复时,它不会起作用

  var delay = 2000 //set delay in miliseconds
  var curindex = 0

  var randomimages = new Array()

  randomimages[0] = "hhh200.jpg"
  randomimages[1] = "ray200.jpg"

  var preload = new Array()

   for (n = 0; n < randomimages.length; n++) {
      preload[n] = new Image()
      preload[n].src = randomimages[n]
  }

  document.write('<img name="defaultimage" src="' + randomimages[Math.floor(Math.random() * (randomimages.length))] + '">')

   function rotateimage() {

      if (curindex == (tempindex = Math.floor(Math.random() * (randomimages.length)))) {
          curindex = curindex == 0 ? 1 : curindex - 1
      } else curindex = tempindex

      document.images.defaultimage.src = randomimages[curindex]
  }

  setInterval("rotateimage()", delay)

有人知道它为什么不起作用吗?

如果您只是在页面的其他位置复制并粘贴它,那么您正在重写变量值,并且您为两个图像指定了相同的名称,因此有两个问题。您应该将其全部放在函数中,并在您想要的两个位置调用该函数

试试这个

function randomImage(randomImages, imageName) {
    var delay = 2000 //set delay in miliseconds
    var curindex = 0  

    var preload = new Array()

    for (n = 0; n < randomImages.length; n++) {
        preload[n] = new Image()
        preload[n].src = randomImages[n]
    }

    document.write('<img name="' + imageName + '" src="' + randomImages[Math.floor(Math.random() * (randomImages.length))] + '">');

    function rotateimage() {
       var tempindex = Math.floor(Math.random() * (randomImages.length));
       if (curindex == tempindex) {
           curindex = curindex == 0 ? 1 : curindex - 1
       } else curindex = tempindex

       document.images[imageName].src = randomImages[curindex];
    }

    setInterval("rotateimage()", delay);
}



// and then to use it, create your image arrays outside of the function, and call the function.

var images1 = new Array();
images1[0] = "hhh200.jpg";
images1[1] = "ray200.jpg";

var images2 = new Array();
images2[0] = "hhh200.jpg";
images2[1] = "ray200.jpg";


randomImage(images1, 'imageOneName');



randomImage(images2, 'imageTwoName');

我还没有测试过这段代码,但这是您应该遵循的总体思路

需要比更多的详细信息,但它无法提供帮助。您的代码正在我的FF 7.0.1上工作。是的,我知道它可以工作,但当我将其复制并粘贴到页面上的其他位置时,所有图像都不会更改?它们都保持文具复制粘贴在同一页上多次可能不起作用,因为使用了相同的变量、数组并相互碰撞