I'm building a custom gallery that utilizes WordPress and Zurb Foundation:
- The template shows a paged archive of custom posts; a custom query populates the page with a grid of linked thumbnails.
- Uses the Reveal modal plugin in Foundation 4, along with WordPress' AJAX to append content to the reveal-modal div.
- Clicking on a
.previous-sketch
,.next-sketch
link in the modal window will bring up the corresponding adjacent post.
I've managed to get the above working, but find that after opening one modal window and cycling through the posts, things start to go out of sync. The dark background, .reveal-modal-bg
is not displayed when a modal window is shown, and things behave somewhat strangely.
I thank you in advance if you have any particular insights into this problem.
UPDATE: I was able to work on this some more, and for the most part it seems to work. Though I feel that my solution isn't the most elegant.
My jQuery code is below.
(function($) {
$.fn.displayPost = function() {
event.preventDefault();
var post_id = $(this).data("id");
var id = "#" + post_id;
// Check if the reveal modal for the specific post id doesn't already exist by checking for it's length
if($(id).length == 0 ) {
// We'll add an ID to the new reveal modal; we'll use that same ID to check if it exists in the future.
var modal = $('<div>').attr('id', post_id ).addClass('reveal-modal').appendTo('body');
var ajaxURL = MyAjax.ajaxurl;
$.ajax({
type: 'POST',
url: ajaxURL,
data: {"action": "load-content", post_id: post_id },
success: function(response) {
modal.empty().html(response).append('<a class="close-reveal-modal">×</a>').foundation('reveal', 'open');
modal.bind('opened', function() {
// Trigger window resize to reset the left margin.
$(window).trigger('resize');
var left;
left = Math.max($(window).width() - $(id).outerWidth(), 0) / 2;
$(id).css({
left:left + $(window).scrollLeft()
});
return false;
});
}
});
}
//If the div with the ID already exists we'll just open it.
else {
$(id).foundation('reveal', 'open');
}
// Recalculate left margin on window resize
$(window).resize(function(){
var left;
left = Math.max($(window).width() - $(id).outerWidth(), 0) / 2;
$(id).css({
left:left + $(window).scrollLeft()
});
});
}
})(jQuery);
// Apply the function when we click on the .reveal link
jQuery(document).on("click", ".reveal,.secondary", function() {
jQuery(this).displayPost();
});
// Use Reveal's built in function to close the div when we click our paging links
jQuery(document).on("click", ".secondary", function() {
var id = jQuery(this).closest("div").attr("id");
jQuery(id).foundation('reveal', 'close');
});