我正在使用 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>');
}
}
}
}
}