Я использую плагин jquery simplegallery (http://www.dynamicdrive.com/dynamicindex4/simplegallery.htm). из коробки, он работает, как написано в их документации. Однако я хочу использовать его таким образом, чтобы получить динамический список случайных изображений с сервера через вызов ajax, так что список меняется каждый раз при загрузке страницы. На сервере у меня есть сценарий оболочки, который находит и перемещает только нужные мне изображения в нужную папку один раз в день. Затем я использую вызов ajax в сценарий php он находит текущий пакет изображений и случайным образом выбирает n # изображений. Скрипт php возвращает строку, смоделированную ниже как «var ouput». Первоначально я пытался заставить скрипт php вернуть массив, но не смог этого сделать, поэтому Я только что собрал строку в скрипте php с двумя разными разделителями и отправил ее обратно. Затем на клиенте с помощью javascript я разделил строку и создал массив images. Однако, похоже, есть разница в массиве, созданном из строка и массив жестко запрограммированы со значениями, как в примере Simpgallery.
Мой код ниже: кто-нибудь знает, что я делаю неправильно?
var rand_pix = [ ["../img/blog-pix/l-1600-1200-c80e69aa-bc3f-4264-823b-fcdf2614f87c-300x225.jpg ", "../img/blog-pix/l-1600-1200-c80e69aa-bc3f-4264-823b-fcdf2614f87c-300x225.jpg ", "", "" ],["../img/blog-pix/l-1600-1200-4bebed86-e7e7-4395-8f01-97ab7f57c3c5-300x225.jpg ", "../img/blog-pix/l-1600-1200-4bebed86-e7e7-4395-8f01-97ab7f57c3c5-300x225.jpg ", "", "" ],["../img/blog-pix/l-1600-1200-f4013041-1ec8-4a5d-b261-1bb9bb330a38-300x225.jpg ", "../img/blog-pix/l-1600-1200-f4013041-1ec8-4a5d-b261-1bb9bb330a38-300x225.jpg ", "", "" ] ]; // hard coded like simple gallery example
var output = "../img/blog-pix/l-1600-1200-a27e5201-e371-4a36-9eeb-68eb5f95efe2-300x225.jpg:../img/blog-pix/l-1600-1200-a27e5201-e371-4a36-9eeb-68eb5f95efe2-300x225.jpg:pix_text1:pix_text2|../img/blog-pix/l-1600-1200-0bc2fec3-5e17-4447-8cff-2f0649100d6f-300x225.jpg:../img/blog-pix/l-1600-1200-0bc2fec3-5e17-4447-8cff-2f0649100d6f-300x225.jpg:pix_text1:pix_text2|../img/blog-pix/l-1600-1200-5b7447cd-ea80-4b7e-9f9e-c0cfc52ef040-300x225.jpg:../img/blog-pix/l-1600-1200-5b7447cd-ea80-4b7e-9f9e-c0cfc52ef040-300x225.jpg:pix_text1:pix_text2|../img/blog-pix/l-1600-1200-83f3544b-235a-415f-b988-393e24f52d71-300x225.jpg:../img/blog-pix/l-1600-1200-83f3544b-235a-415f-b988-393e24f52d71-300x225.jpg:pix_text1:pix_text2|../img/blog-pix/l-640-480-5db9395b-0394-4c14-9092-8bb001f3912e-300x225.jpg:../img/blog-pix/l-640-480-5db9395b-0394-4c14-9092-8bb001f3912e-300x225.jpg:pix_text1:pix_text2|"; //simulate what is recieved back from the server
var out_list = output.split("|");
var images = []; // dynamic array of arrays that simplge gallry can use
var img_stuff = []; // temp array holder for the four things each image needs
//alert(out_list.length);
for (var i = 0; i<out_list.length-1; i++){
var out_list_items = out_list[i].split(":");
for(var p = 0; p<out_list_items.length; p++){
var items = out_list_items[p].split(":"); // this array holds each sub array of items for an image that can then be added to list array ..
img_stuff.push(items);
}
images.push(img_stuff);
img_stuff = [];
}
alert(rand_pix.constructor); -> function Array() { [native code] }
alert(images.constructor); - > function Array() { [native code] }
Однако работают следующие:
var mygallery=new simpleGallery({
wrapperid: "simplegallery1", //ID of main gallery container,
dimensions: [300, 225], //width/height of gallery in pixels. Should reflect dimensions of the images exactly
imagearray: rand_pix,
autoplay: [true, 2500, 2], //[auto_play_boolean, delay_btw_slide_millisec, cycles_before_stopping_int]
persist: false, //remember last viewed slide and recall within same session?
fadeduration: 4500, //transition duration (milliseconds)
oninit:function(){ //event that fires when gallery has initialized/ ready to run
//Keyword "this": references current gallery instance (ie: try this.navigate("play/pause"))
},
onslide:function(curslide, i){ //event that fires after each slide is shown
//Keyword "this": references current gallery instance
//curslide: returns DOM reference to current slide's DIV (ie: try alert(curslide.innerHTML)
//i: integer reflecting current image within collection being shown (0=1st image, 1=2nd etc)
}
});
Хотя это не (выдает ошибку jquery: узел не может быть вставлен в указанной точке иерархии "code:" 3 [Break on this error] возвращает jQuery (context) .find ( селектор);} е ... y.clean ([container.innerHTML]) [0];} еще)
var mygallery=new simpleGallery({
wrapperid: "simplegallery1", //ID of main gallery container,
dimensions: [300, 225], //width/height of gallery in pixels. Should reflect dimensions of the images exactly
imagearray: images,
autoplay: [true, 2500, 2], //[auto_play_boolean, delay_btw_slide_millisec, cycles_before_stopping_int]
persist: false, //remember last viewed slide and recall within same session?
fadeduration: 4500, //transition duration (milliseconds)
oninit:function(){ //event that fires when gallery has initialized/ ready to run
//Keyword "this": references current gallery instance (ie: try this.navigate("play/pause"))
},
onslide:function(curslide, i){ //event that fires after each slide is shown
//Keyword "this": references current gallery instance
//curslide: returns DOM reference to current slide's DIV (ie: try alert(curslide.innerHTML)
//i: integer reflecting current image within collection being shown (0=1st image, 1=2nd etc)
}
});
Помимо решения моей проблемы (как динамически создать массив, который будет принимать простая галерея), мне интересно понять, что отличает массив, который я строю, путем анализа строки по сравнению с жестко закодированным массивом [["blah", " blah "," img1 "" img1 "], [" blah2 "," blah2 "," img2 "" img2 "]].
Есть мысли? предложения о лучшем способе сделать это также приветствуются .... Спасибо