Skip to content

Instantly share code, notes, and snippets.

@AndrewSepic
Created February 23, 2016 19:06
Show Gist options
  • Save AndrewSepic/1b12fd0afd68e1e0f9ef to your computer and use it in GitHub Desktop.
Save AndrewSepic/1b12fd0afd68e1e0f9ef to your computer and use it in GitHub Desktop.
function fireModal(){
// Creates Modal window Markup
var modal = (function(){
var
method = {},
$overlay,
$modal,
$content,
$close;
// Center the modal in the viewport
method.center = function () {
var top, left;
top = Math.max($(window).height() - $modal.outerHeight(), 0) / 2;
left = Math.max($(window).width() - $modal.outerWidth(), 0) / 2;
$modal.css({
top:top + $(window).scrollTop(),
left:left + $(window).scrollLeft()
});
};
// Open the modal
method.open = function (settings) {
$content.append(settings.content);
$modal.css({
width: settings.width || 'auto',
//width: settings.width || 'auto',
height: settings.height || 'auto',
margin: settings.margin || '2rem'
});
method.center();
$(window).bind('resize.modal', method.center);
$modal.show();
$overlay.show();
};
// Close the modal
method.close = function () {
$modal.hide();
$overlay.hide();
$content.empty();
$(window).unbind('resize.modal');
};
// Generate the HTML and add it to the document
$overlay = $('<div id="overlay"></div>');
$modal = $('<div id="modal"></div>');
$content = $("<div id='content'></div>");
$close = $('<a id="close" href="#">X</a>');
$modal.hide();
$overlay.hide();
$modal.append($content, $close);
$(document).ready(function(){
$('body').append($overlay, $modal);
});
$close.click(function(e){
e.preventDefault();
method.close();
});
return method;
}());
// Call Modal for Single events
$("div.enrollment .more-info a").click(function(e){
e.preventDefault();
// Opens modal with description area as content
var htmlString = $(this).parent().siblings("h2.healcode-enrollment-name").clone().html() + $(this).parent().siblings("div.healcode-description-area").clone().show().html();
modal.open({
content : htmlString
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment