Skip to content

Instantly share code, notes, and snippets.

@lukeandersen
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save lukeandersen/ab3381ccb76d25c2196f to your computer and use it in GitHub Desktop.

Select an option

Save lukeandersen/ab3381ccb76d25c2196f to your computer and use it in GitHub Desktop.
Dynamic image galleries
// Dynamic project image galleries
$('.project-item-wrapper').click(function(e) {
e.preventDefault();
// .project-item-wrapper should have the page destination as its link src, this way is JS is disabled the user would still get to the images
var link = $(this).attr('href');
$.ajax({
url: link,
success: function( data ) {
// Get project title
var projectTitle = $(data).find('.section-title').html().toUpperCase();
// Set empty array to push images to
var projectImgs = [];
$(data).find('[rel=lightbox]').each(function(){
// Get image attr's
var imgURL = $(this).attr('href');
var imgCaption = $(this).next('.wp-caption-text').html();
// If no image caption provided set the attr to and empty string
if (imgCaption == undefined) {
imgCaption = "";
}
// Push images, title and caption to array
projectImgs.push({
'href' : imgURL,
'title' : '<span>'+ projectTitle +'</span>'+ imgCaption
});
});
// console.log(projectImgs);
// Call lightbox with array as the source (requires fancybox2)
$.fancybox.open(
projectImgs,
{
padding: 0,
helpers: {
title : {
type : 'over'
}
}
}
);
}
});
});
@lukeandersen
Copy link
Copy Markdown
Author

I can't believe it took me so long to make this... I still need to add error states.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment