1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| (function($) {
$.modal = function (config) {
var defaults, options, container, header, close, content, title, overlay;
defaults = {
title: ''
, html: ''
, ajax: ''
, width: null
, overlay: true
, overlayClose: false
, escClose: true
};
options = $.extend (defaults, config);
container = $('<div>', { id: 'modal' });
header = $('<div>', { id: 'modalHeader' });
content = $('<div>', { id: 'modalContent' });
overlay = $('<div>', { id: 'overlay' });
title = $('<h2>', { text: options.title });
close = $('<a>', { 'class': 'close', href: 'javascript:;', html: '×' });
container.appendTo ('body');
header.appendTo (container);
content.appendTo (container);
if (options.overlay) {
overlay.appendTo ('body');
}
title.prependTo (header);
close.appendTo (header);
if (options.ajax == '' && options.html == '') {
title.text ('No Content');
}
if (options.ajax !== '') {
content.html ('<div id="modalLoader"><img src="./img/ajax-loader.gif" /></div>');
$.modal.reposition ();
$.get (options.ajax, function (response) {
content.html (response);
$.modal.reposition ();
});
}
if (options.html !== '') {
content.html (options.html);
}
close.bind ('click', function (e) {
e.preventDefault ();
$.modal.close ();
});
if (options.overlayClose) {
overlay.bind ('click', function (e) { $.modal.close (); });
}
if (options.escClose) {
$(document).bind ('keyup.modal', function (e) {
var key = e.which || e.keyCode;
if (key == 27) {
$.modal.close ();
}
});
}
$.modal.reposition ();
}
$.modal.reposition = function () {
var width = $('#modal').outerWidth ();
var centerOffset = width / 2;
$('#modal').css ({ 'left': '50%', 'top': $(window).scrollTop () + 75, 'margin-left': '-' + centerOffset + 'px' });
};
$.modal.close = function () {
$('#modal').remove ();
$('#overlay').remove ();
$(document).unbind ('keyup.modal');
} |
Partager