0

我有一个同时显示两个项目的画廊,但我想知道是否有比一直为画廊中的项目 B 重复相同的代码更优雅的方法。

这是主要代码:

var Gallery = new Object();
    window.onload = function(){
Gallery.Images = ['red','blue','pink','green','yellow','purple','orange','navy'];
Gallery.CurrentIndexA = 0;
    Gallery.CurrentIndexB = 1;
 };

Gallery.Next = function(){
if (Gallery.CurrentIndexA < (Gallery.Images.length-1)){ 
    Gallery.CurrentIndexA++;
    Gallery.CurrentIndexB++;
    console.log("A is:" + Gallery.CurrentIndexA);
    console.log("B is:" + Gallery.CurrentIndexB);
} 
else {
    Gallery.CurrentIndexA = 0;
}
Gallery.Display();
};

Gallery.Prev = function(){
if (Gallery.CurrentIndexA > 0){
    Gallery.CurrentIndexA--;
    Gallery.CurrentIndexB--;
    console.log("A is:" + Gallery.CurrentIndexA);
    console.log("B is:" + Gallery.CurrentIndexB);
}
else {
    Gallery.CurrentIndexA = (Gallery.Images.length-1); 
}
Gallery.Display();
};

Gallery.Display = function(){
var photoA = document.getElementById('photoA');
var photoB = document.getElementById('photoB');
var currentImageA = Gallery.Images[Gallery.CurrentIndexA];
var currentImageB = Gallery.Images[Gallery.CurrentIndexB];
photoA.className = currentImageA;
photoB.className = currentImageB;
};

这是一个小提琴:http: //jsfiddle.net/2AdA9/1/

非常感谢!

4

1 回答 1

0

如果你想让它看起来更好,你可以做的一件事就是这样。

Gallery.moveUp = function (){
    Gallery.CurrentIndexA += 1;
    Gallery.CurrentIndexB += 1;
};

Gallery.moveDown = function (){
    Gallery.CurrentIndexA -= 1;
    Gallery.CurrentIndexB -= 1;
};

然后,当您想向上或向后移动时,只需调用这些函数。

于 2013-10-30T13:16:22.947 回答