扩展用于旋转木马/图像循环功能的JavaScript

扩展用于旋转木马/图像循环功能的JavaScript,javascript,Javascript,我一直在尝试修改以下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://m

我一直在尝试修改以下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>

var掉期=0;
var homeimg=新数组(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(函数(){
交换=(交换?0:1);
$(“.homepage main image img”).attr(“src”,homeimg[swap]);
},4000);
请允许我问一下,为什么在我尝试扩展此脚本以容纳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>

var掉期=0;
var homeimg=新阵列(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(函数(){
交换=(交换?0:8);
$(“.homepage main image img”).attr(“src”,homeimg[swap]);
},1500);
swap=(swap?0:8)

这意味着它只是在第一个图像和最后一个图像之间交换。如果您想循环浏览所有这些内容,请尝试以下方法


swap=(swap>=8?0:++swap)

我想你在期待
swap=(swap?0:8)以充当在0和8之间更改它的角色,但该行不会这样做。如果swap不同于0,它将变为0,如果是0,它将变为8。基本上,您只交换第一个和最后一个图像。你想要的是:

swap = (swap + 1) % (homeimg.length + 1);

您正在初始化元素太少的数组<代码>数组(7)
创建一个包含7个元素的数组,但您有8个图像。此外,您应该设置
swap=(swap?0:7)
@Solomon这不是真正的问题比我的更好的解决方案,如果阵列在将来改变长度会更容易,而且因为它都是算术的,所以它也避免了错过性能的分支预测。很好。@KarlReid在可能的情况下避免常数总是很好的,但是你的可能更容易理解