2016-08-03 52 views
4

我有一個畫廊設置與燈箱插件lightGallery阿賈克斯JSON數據和燈箱衝突

該畫廊完美的靜態HTML。當我動態獲取API數據並試圖讓燈箱在這些項目上工作時,問題就出現了。

我似乎無法正確地得到另一個收藏到工作都具有這種功能負載的HTML塊從頁面(加載的被動態生成的)。如果我可以解決衝突,此應用會執行正確的HTML抓取。

有沒有初步的想法?任何人遇到類似的東西?

JS:

//----------------------------------------------------------------// 
//---------------// Calling Lightgallery //---------------// 
//----------------------------------------------------------------// 
    $('#photoBox').lightGallery({ 
     selector: '.tile', 
     download: false, 
     counter: false, 
     zoom: false, 
     thumbnail: false, 
     mode: 'lg-fade' 
    }); 

// ----------------------------------------------------------------// 
// ---------------// Unsplash Photos //---------------// 
// ----------------------------------------------------------------// 

// Filter Difference based on button click 
$('button').click(function() { 
    $('button').removeClass("active"); 
    $(this).addClass("active"); 
    var unsplashAPI = "#URL"; 
    var order = $(this).text(); 
    var sortOptions = { 
     order_by: order, 
     format: "json" 
    }; 
    function displayPhotos(data) { 
    var photoData = ''; 
     $.each(data, function (i, photo) { 
      photoData += '<a class="tile" data-src="' + photo.urls.regular + '">' + '<img alt class="photo" src="' + photo.urls.regular + '">' + '<div class="caption-box" id="' + photo.id + '">' + '<h1 class="photo-title">' + 'Photo By: ' + photo.user.name + '</h1>' + '<p class="photo-description"> Description: Awesome photo by ' + photo.user.name + ' aka: ' + photo.user.username + ' So far this photo has ' + '<span>' + photo.likes + '</span>' + ' Likes' + '</p>' + '</div>' + '</a>'; 
     }); 
     $('#photoBox').html(photoData); 
    } 
$.getJSON(unsplashAPI, sortOptions, displayPhotos); 
}); // End button 

HTML:

<div class="content" id="photoBox"></div> 

- 感謝

回答

0

調用插件的數據附加到頁面

function displayPhotos(data) { 
    var photoData = ''; 
     $.each(data, function (i, photo) { 
      photoData += '<a class="tile" data-src="' + photo.urls.regular + '">' + '<img alt class="photo" src="' + photo.urls.regular + '">' + '<div class="caption-box" id="' + photo.id + '">' + '<h1 class="photo-title">' + 'Photo By: ' + photo.user.name + '</h1>' + '<p class="photo-description"> Description: Awesome photo by ' + photo.user.name + ' aka: ' + photo.user.username + ' So far this photo has ' + '<span>' + photo.likes + '</span>' + ' Likes' + '</p>' + '</div>' + '</a>'; 
     }); 
     $('#photoBox').html(photoData); 
     $('#photoBox').lightGallery({ 
     selector: '.tile', 
     download: false, 
     counter: false, 
     zoom: false, 
     thumbnail: false, 
     mode: 'lg-fade' 
    }); 
    }