I have a page that loads photos on page load from an API.
** Edit: Here is a CodePen with the page, error happing there: http://codepen.io/nathan-anderson/pen/GqXbvK
When initially loading the page I use this to call on the info and use the lightgallery plugin:
// ----------------------------------------------------------------//
// ---------------// Unsplash Photos //---------------//
// ----------------------------------------------------------------//
function displayPhotos(data) {
var photoData = '';
$.each(data, function (i, photo) {
photoData += '<a class="tile"' + 'data-sub-html="#' + photo.id + '"'+ '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:' + '<a target="_blank" href="' + photo.links.html + '">' + photo.user.username + ')</a>' + ' So far this photo has ' + '<span>' + photo.likes + '</span>' + ' Likes.' + ' You can download this photo if you wish, it has a free <a target="_blank" href="https://unsplash.com/license"> Do whatever you want </a> license. <a target="_blank" href="' + photo.links.download + '"><i class="fa fa-download" aria-hidden="true"></i> </a> </p>' + '</div>' + '</a>';
});
// Putitng into HTML
$('#photoBox').html(photoData);
//--------//
// Calling Lightbox
//--------//
$('#photoBox').lightGallery({
selector: '.tile',
download: false,
counter: false,
zoom: false,
thumbnail: false,
mode: 'lg-fade'
});
} // End Displayphotos function
// Show popular photos on pageload
$.getJSON(unsplashAPI, popularPhotos, displayPhotos);
I have multiple buttons for showing different orders of the photos. The API grabs different photos based on the "order_by" option.
This is my code to show the different sections:
var popularPhotos = {
order_by: "popular",
format: "json"
};
var latestPhotos = {
order_by: "latest",
format: "json"
};
var oldestPhotos = {
order_by: "oldest",
format: "json"
};
// Button Click Changes
$('button').click(function () {
$('button').removeClass("active");
$(this).addClass("active");
}); // End button
// Show Popular Photos
$('#popular').click(function () {
$.getJSON(unsplashAPI, popularPhotos, displayPhotos);
}); // End button
// Show latest Photos
$('#latest').click(function () {
$.getJSON(unsplashAPI, latestPhotos, displayPhotos);
}); // End button
// Show oldest Photos
$('#oldest').click(function () {
$.getJSON(unsplashAPI, oldestPhotos, displayPhotos);
}); // End button
This does load the new photos on button click but it breaks the function of the lightbox plugin.
Any thoughts? Anyone else running into this?
-- Thanks