扩展用于轮播/图像循环功能的JavaScript

时间:2017-05-11 16:29:37

标签: javascript

我一直在尝试修改以下代码片段,循环显示2张图片:

 <script type="text/javascript">
        var swap = 0;
        var homeimg = new Array(1);
        homeimg[0] = "http://media.com/i/ct/11-hp-main?w=2350&h=1056&qlt=50";
        homeimg[1] = "http://media.com/i/ct/11-hp-main-mens?w=2350&h=1056&qlt=50";
        setInterval(function() {
          swap = (swap ? 0 : 1);
          $(".homepage-mainimage img").attr("src",homeimg[swap]);
        },4000);
    </script>

请问为什么在我尝试扩展此脚本以容纳8个图像后,为什么它不起作用?我很难理解为什么脚本已经坏了。

  <script type="text/javascript">
        var swap = 0;
        var homeimg = new Array(7);
        homeimg[0] = "http://media.com/i/ct/15-hp-main-1?w=2350&h=1056&qlt=50";
        homeimg[1] = "http://media.com/i/ct/15-hp-main-2?w=2350&h=1056&qlt=50";
        homeimg[2] = "http://media.com/i/ct/15-hp-main-3?w=2350&h=1056&qlt=50";
        homeimg[3] = "http://media.com/i/ct/15-hp-main-4?w=2350&h=1056&qlt=50";
        homeimg[4] = "http://media.com/i/ct/15-hp-main-5?w=2350&h=1056&qlt=50";
        homeimg[5] = "http://media.com/i/ct/15-hp-main-6?w=2350&h=1056&qlt=50";
        homeimg[6] = "http://media.com/i/ct/15-hp-main-7?w=2350&h=1056&qlt=50";
        homeimg[7] = "http://media.com/i/ct/15-hp-main-8?w=2350&h=1056&qlt=50";
        setInterval(function() {
          swap = (swap ? 0 : 8);
          $(".homepage-mainimage img").attr("src",homeimg[swap]);
        },1500);
    </script>

0 个答案:

没有答案