/ Published in: JavaScript
For centering, you will want to combine the efforts of CSS Width, CSS Margin-Left and Absolute Positioning (50%). (Analogously with Height and Margin-Top.)
Expand |
Embed | Plain Text
<!-- Markup --> <h1 class="grid_13 omega">(dev) Modal Window Test</h1> <div id="main-content" class="grid_9"> <ul class="no-margin"> <li> Initial MW should not be modal </li> <li><a id="trigger_1" href="" title="" class='modal_open:modal-window-1:{"css":{"background":"#c30","height":"300px","margin-left":"-230px","top":"160px","left":"50%"},"js":{"fadeSpeed":"fast","trulyModal":"false"},"content":{"windowTitle":"Meta title"}}'>Open Modal Window 1 (options set as <code>@class</code> metadata and loaded as JSON)</a> (fast, modal)</li> <li><a id="trigger_2" href="" title="I have a title!" class='modal_open:modal-window-2'>Open Modal Window 2 (options pass'd as arguments)</a> (slow, not modal)</li> </ul> <div id="modal-window-1" class="modal-window-container grid_8 hide"> <div class="inner_2"> <div class="clearfix"><a href="" title="" class="modal-window-close modal-window-1 align-right">Close Window</a></div> <h2 class="header_4">Address Verification</h2> <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed massa est, aliquam in adipiscing sit amet, tincidunt pellentesque ipsum.</p> <form id="css-address-verification-form" name="" action="" method="post"> <ul class="no-margin clearfix tail_3"> <li class="grid_4"> <input type="radio" class="radio" value="" id="css-original-address" name="" /> <label for="css-original-address">Original Address</label> <div class="adr indent-5"> <div class="street-address">1021 Main St.</div> <div class="extended-address">Suite NN</div> <span class="locatity">Houston</span> <span class="region">TX</span> <span class="postal-code">77002</span> <div class="country-name">United States</div> </div> </li> <li class="grid_4"> <input type="radio" class="radio" value="" id="css-best-match-address" name="" /> <label for="css-best-match-address">Best Match</label> <div class="adr indent-5"> <div class="street-address">1021 Main St.</div> <div class="extended-address">Suite NN</div> <span class="locatity">Houston</span> <span class="region">TX</span> <span class="postal-code">77002</span> <div class="country-name">United States</div> </div> </li> </ul> </form> <a href="" title="" class="button-link">Continue</a> <a href="" title="" class="modal-window-close modal-window-1">Cancel</a> </div> </div> <div id="modal-window-2" class="modal-window-container grid_8 hide"> <div class="modal-window-title">Title</div> <div class="inner_2"> <div class="clearfix"><a href="" title="" class="modal-window-close modal-window-1 align-right">Close Window</a></div> <h2 class="header_4">jQuery Modal Window</h2> <p>Loads of fun with jQuery!</p> </div> </div> </div><!-- #main-content --> <div id="right-rail" class="grid_4 omega"> <p>Right Rail</p> </div><!-- #right-rail --> <!-- CSS (not tested in IE6) --> #overlay { position: absolute; height: 100%; width: 100%; background: #000; top: 0; left: 0; z-index: 9000; } .modal-window-container { position: absolute; top: 50%; left: 50%; background: #fff; z-index: 9001; } <!-- JavaScript --> ;(function($) { // modal window plugin $.modalWindow = function(options_args) { var undefined = "undefined", mw = this; mw.elem_container, mw.content_settings, mw.css_settings, mw.js_settings; function sparkModal(elem_container, css_settings, js_settings, content_settings) { if (js_settings.onPageLoad === true && js_settings.animateOverlay !== true) { $('<div id="overlay" />').appendTo("body") } else { $('<div id="overlay" class="hide" />') .appendTo("body") .fadeIn(js_settings.fadeSpeed.toString()) .removeClass("hide"); } elem_container.find(".modal-window-title").text(content_settings.windowTitle); elem_container .css(css_settings) .fadeIn(js_settings.fadeSpeed.toString()) .removeClass("hide"); mw.elem_container = elem_container; mw.content_settings = content_settings; mw.css_settings = css_settings; mw.js_settings = js_settings; } $("#overlay,.modal-window-close").live("click", function(e) { if ( (mw.js_settings.trulyModal !== true && mw.js_settings.trulyModal !== "true") || $(this).hasClass("modal-window-close") ) { $(".modal-window-container").fadeOut(); $("#overlay").fadeOut().queue(function() { $("#overlay").remove(); $("#overlay").dequeue(); // reset container mw.elem_container .addClass("hide") .attr("style", ""); }); } e.preventDefault(); }); return { sparkModal: sparkModal, triggers: $("a[class^='modal_open']").each(function(index, e) { // trigger var trigger = $(this), // trigger id trigger_id = trigger.attr("id"), // defaults options_defaults = { css: { "background":"orange", "position":"absolute", "top":"50%", "left":"50%", "height":"300px", "margin-left":"-235px", "margin-top":"-150px" }, js: { fadeSpeed: "defaults", trulyModal: true, onPageLoad: true }, content: { windowTitle: "Modal Windows!" } }, // get @class of trigger elem class_val = trigger.attr("class") || "", // extract elem container name // if no name specified on any anchor, plugin // should find arbitrary container and load it on page load find_elem = class_val.match(/:([A-Za-z0-9-]+)(:|)/), // find matching content container defined in trigger elem_container = (find_elem) ? $("#"+find_elem[1]) : $("#modal-window-1"), find_class_metaoptions = class_val.match(/{(.*)}/) || {}, // pull settings from trigger options_meta = $.parseJSON(find_class_metaoptions[0]) || {}; trigger.bind("click."+trigger_id, function(e) { // apply css and js or set defaults var css_settings = options_meta.css || options_args.css || options_defaults.css, js_settings = options_meta.js || options_args.js || options_defaults.js; content_settings = options_meta.content || options_args.content || options_defaults.content; sparkModal(elem_container, css_settings, js_settings, content_settings); e.preventDefault(); }); }) }; }; // $.modalWindow })(jQuery); <!-- Invoking --> jQuery(function($) { // testing with args $.modalWindow({ //arguments as defaults for all windows css: { "background":"#fff", "position":"absolute", "top":"50%", "left":"50%", "width":"200px", "height":"200px", "margin-left":"-100px", "margin-top":"-100px" }, js: { fadeSpeed: "slow", trulyModal: true, onPageLoad: false, animateOverlay: true }, content: { windowTitle: "mmmmodwdawdaal!" } }).sparkModal($("#modal-window-1"), { // onload window "background":"#fff", "position":"absolute", "top":"50%", "left":"50%", "width":"200px", "height":"200px", "margin-left":"-100px", "margin-top":"-100px" }, { fadeSpeed: "slow", trulyModal: true, onPageLoad: false, animateOverlay: true }, { windowTitle: "onload!" }); });
You need to login to post a comment.
