/ Published in: JavaScript
Another popup jQuery plugin implementation complying with YAGNI.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
(function() { /** * overlay element */ var mask = null; /** * create mask if it dose not exist */ function init() { mask || (mask = $('<div />', { 'id': 'mask' }).appendTo('body')); } /** * display modal */ function modal() { mask.css({ 'width': $(document).width(), 'height': $(document).height() }); mask.fadeIn('fast'); } /** * display modal and open popup window */ function open() { var popup = $(this), win = $(window), top = win.height() / 2 - popup.height() / 2, left = win.width() / 2 - popup.width() / 2; modal(); popup.css({ 'top': top, 'left': left }); popup.fadeIn('fast'); } /** * close modal and popup window */ function close() { var popup = $(this); popup.hide(); mask.hide(); } /** * initialize popup plugin or execute method * // initialize * var popup = $('#popup').popup(); * // open popup * popup.popup('open'); */ $.fn.popup = function() { var methods = { open: open, close: close }, m = methods[arguments[0]]; if (m) { // method call m.apply(this, arguments); } else { // initialization init.apply(null, arguments); } return this; }; }());