我認爲你可以通過點擊事件啓動畫廊來實現這一點。如果您將此設爲委託事件,則它也會在新創建的圖像上觸發。然後,只需在觸發點擊事件並啓動畫廊時創建圖像數組。
你的圖像應該加入這樣的:
<img class="myAjaxLoadedImage" src="myAjaxLoadedImage1_thumbnail.jpg" alt=""
data-img-title="My title 1" data-img-src="myAjaxLoadedImage1.jpg"
data-img-width="800" data-img-height="600">
<img class="myAjaxLoadedImage" src="myAjaxLoadedImage2_thumbnail.jpg" alt=""
data-img-title="My title 2" data-img-src="myAjaxLoadedImage2.jpg"
data-img-width="400" data-img-height="700">
...
然後是JS將是:
(function($) {
var pswp;
$(function() {
pswp = $('.pswp')[0];
setGalleryClickEvents();
});
function setGalleryClickEvents() {
$(document).on('click','.myAjaxLoadedImage',function(e) {
if (pswp) {
var options = {
index: $(this).index()
// + other PhotoSwipe options here...
}
var images = [];
$('.myAjaxLoadedImage').each(function() {
var $img = $(this);
images.push({
src: $img.data('imgSrc'),
w: $img.data('imgWidth'),
h: $img.data('imgHeight'),
title: $img.data('imgTitle')
});
});
var gallery = new PhotoSwipe(pswp, PhotoSwipeUI_Default, images, options);
gallery.init();
}
});
}
})(jQuery);
謝謝您的回答。你的ajax在哪裏? – John
您是否有一個示例可以動態添加縮略圖而不是圖片? – John
我添加的HTML示例其實就是縮略圖。在模式中可見的圖像是'src'屬性中的圖像:'src =「myAjaxLoadedImage1_thumbnail.jpg」'。這應該是縮略圖的網址。 'data-img-src'的值表示全尺寸圖片的URL,當您點擊縮略圖時顯示:'data-img-src =「myAjaxLoadedImage1.jpg」'。 – vi5ion