我正在使用jquery創建自定義圖庫,使用fancybox,現在我遇到了問題。無論何時用戶點擊下一個/上一個按鈕,我都會調用一個javascript方法,它將檢索有關該照片的所有細節並動態顯示它。但用目前的方法,我無法實現圖像的無限循環。圖庫中的循環圖像
例如,如果我們有10個圖像,並且當用戶在第10個圖像之後點擊下一個圖像時,它應該轉到第一個圖像,並且循環也按照前一個按鈕的方式繼續。我盡我所能,但我無法弄清楚。我的代碼是
photoArray是一個JSON數組,它包含我將在庫中顯示的所有照片的信息。
function showNextPrevious(value) {
var presentPhotoId = $('#presentId').val();
var length = photoArray.length;
if(value == "next") {
for(var i=0; i<photoArray.length; i++) {
if(i!=length-1) {
var id = photoArray[i].photo_id;
if(id == presentPhotoId) {
var tags = (typeof photoArray[i+1].tags!= "undefined")?photoArray[i+1].tags:"";
var caption = photoArray[i+1].caption;
var source = photoArray[i+1].owner;
var mainPhoto = photoArray[i+1].main_photo;
var photoId = photoArray[i+1].photo_id;
var path = photoArray[i+1].path;
var mediumpath = replaceAll(path,".jpg", "_medium.jpg");
var ownerlink = photoArray[i+1].source_link;
var owner = photoArray[i+1].owner;
$('#tag').text(tags);
$('#caption').text(caption);
$("#photoCount").text(((i+1)+1)+"/"+photoArray.length);
$('#presentId').val(photoId);
$('#owner').text(owner);
$('#mainphoto').attr("src" , mediumpath);
if(owner!='NULL' || owner.length>0) {
$('#ownerlink').attr("href" , "http://flickr.com/search/people/?q="+owner);
$('#ownerlink').attr("target" , "_blank");
} else {
$('#ownerlink').attr("href" , "javascript:void('0')");
$('#ownerlink').attr("target" , "_self");
}
if(mainPhoto == "Yes"){
$('#mainP').html('<div style="display:block;color:#000000;">Currently the main photo for this city.</div>');
} else {
$('#mainP').html('<div style="display:block;"><input name="" type="button" class="button-small" value="Make this the main photo for this city" onclick="javascript:changeMainPhoto(\''+photoArray[i+1].photo_id+'\','+photoArray[i+1].city+')" /></div>');
}
} // End of if(id == presentPhotoId)
}
}
} else if(value == "previous") {
for(var i = 0;i<photoArray.length;i++){
var id = photoArray[i].photo_id;
if(id == presentPhotoId){
var tags = (typeof photoArray[i-1].tags!="undefined")?photoArray[i-1].tags:"";
var caption = photoArray[i-1].caption;
var source = photoArray[i-1].owner;
var mainPhoto = photoArray[i-1].main_photo;
var photoId = photoArray[i-1].photo_id;
var path = photoArray[i-1].path;
var mediumpath = replaceAll(path,".jpg", "_medium.jpg");
var ownerlink = photoArray[i-1].source_link;
var owner = photoArray[i-1].owner;
$('#tag').text(tags);
$('#caption').text(caption);
$('#presentId').val(photoId);
$('#owner').text(owner);
$("#photoCount").text(((i-1)+1)+"/"+photoArray.length);
$('#mainphoto').attr("src" , mediumpath);
if(owner!='NULL' || owner.length>0) {
$('#ownerlink').attr("href" , "http://flickr.com/search/people/?q="+owner);
$('#ownerlink').attr("target" , "_blank");
} else {
$('#ownerlink').attr("href" , "javascript:void(0)");
$('#ownerlink').attr("target" , "_self");
}
if(mainPhoto == "Yes") {
$('#mainP').html('<div style="display:block;">Currently the main photo for this city.</div>');
} else {
$('#mainP').html('<div style="display:block;"><input name="" type="button" class="button-small" value="Make this the main photo for this city" onclick="javascript:changeMainPhoto(\''+photoArray[i-1].photo_id+'\','+photoArray[i-1].city+')" /></div>');
}
}
}
}
}
對不起,問這個問題,但我不能複製的價值,並將其添加到JSON數組中,因爲沒有辦法在現有的JSONArray中追加一個新的行在JavaScript中,沒有將其構建爲字符串。所以如果你給我一個這樣的解決方案,會很高興。 – Rajkumar
查看'.push()'和'.apply()'方法 –
感謝這個想法,它幫了很大忙。 – Rajkumar