Return to Snippet

Revision: 59078
at August 16, 2012 20:37 by p-baleine


Initial Code
(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;
  };
}());

Initial URL


Initial Description
Another popup jQuery plugin implementation complying with YAGNI.

Initial Title
jQuery popup plugin

Initial Tags
plugin, jquery

Initial Language
JavaScript