2013-10-30 57 views
0

我有這個畫廊,一次顯示兩個項目,但我想知道是否有一個更優雅的方式比在圖庫中的項目B重複相同的代碼。兩個項目在同一時間的Javascript畫廊

這裏的主要代碼:

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/

非常感謝!

+0

你的小提琴不起作用。 – j0hnstew

回答

0

如果你想讓它看起來更好,你可以做的一件事就是這樣。

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

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

然後當你想要向上移動或後退時調用這些函數。