This example walks you through basics of creating and positioning an Overlay. It walks you through setting up the sandbox environment for the Overlay, including the required modules, and instantiating the Overlay based on markup already on the page.
It also provides a couple of input fields, allowing you to invoke the Overlay's move()
method, to move the Overlay to a specific XY position on the page.
To create an instance of an Overlay on your page, the only module you need to request is the overlay
module. The overlay
module will pull in the widget
, widget-stack
, widget-position
, widget-position-align
, widget-position-constrain
and widget-stdmod
extensions it uses.
YUI({...}).use("overlay", function(Y) { // We'll write example code here, where we have a Y.Overlay class available. });
Note, using the overlay
module, will also pull down the default CSS required for overlay, on top of which we only need to add our required look/feel CSS for the example.
Note: be sure to add the yui3-skin-sam
classname to the
page's <body>
element or to a parent element of the widget in order to apply
the default CSS skin. See Understanding Skinning.
<body class="yui3-skin-sam"> <!-- You need this skin class -->
For this example, we'll create the overlay instance from markup which already exists on the page, and is shown below. We mark the existing markup with the yui3-overlay-loading
class, so that we can hide it while the rich control is being instantiated:
<div id="overlay" class="yui3-overlay-loading"> <div class="yui3-widget-hd">Overlay Header</div> <div class="yui3-widget-bd">Overlay Body</div> <div class="yui3-widget-ft">Overlay Footer</div> </div>
The container DIV with id="overlay" is specified as the contentBox for the Overlay instance, and during instantiation, the overlay will look for DIV's marked with the yui3-widget-hd, yui3-widget-bd, yui3-widget-ft
classes to setup the Overlay's header, body and footer content attributes.
To create an overlay instance, we use the overlay constructor Y.Overlay
, and pass it the contentBox
node reference for the existing markup on the page. We also set a height/width for the overlay and the initial position for the Overlay (which otherwise defaults to 0,0):
// Create an overlay from markup, using an existing contentBox. var overlay = new Y.Overlay({ srcNode:"#overlay", width:"13em", height:"10em", xy:[xy[0] + 30, xy[1] + 38] }); overlay.render();
After creating the overlay instance, we invoke overlay.render()
to update the DOM to reflect the current state of the overlay. Before render is called, the state of the Overlay should not be reflected in the DOM (for example, we can change the height, without it being reflected in the DOM. When we finally render, the current height value will be applied to the DOM). We could also pass an optional node reference to the render method, to have the overlay rendered under a different parent node, from where the content box currently lies.
Overlays have support for basic page based XY positioning. This example provides a couple of input controls which can be used to move the overlay to a specific XY page co-ordinate. Later examples will show how Overlay's extended positioning support to align/center the Overlay relative to other elements on the page.
var xInput = Y.one("#x"); var yInput = Y.one("#y"); Y.on("click", function(e) { var x = parseInt(xInput.get("value")); var y = parseInt(yInput.get("value")); overlay.move(x,y); }, "#move");
Overlay can be moved by invoking the move
method, with either separate x and y arguments (move(100,200)
), or as an array (move([100,200])
). The x, y and xy
attributes can also be used to move the overlay, and are equivalent to the move method (overlay.set("x", 200);overlay.set("xy", [100,200])
)
A select dropdown is added to the example page, along with additional content, to demonstrate the Overlay's ability to provide stacking and shimming support (to block select dropdown bleed through in IE6).
The overlay.css Sam Skin file (build/overlay/assets/skins/sam/overlay.css) provides the default functional CSS for the overlay. Namely the CSS rules to hide the overlay, and position it absolutely. However there's no default out-of-the-box look/feel applied to the Overlay widget.
The example provides its own look and feel for the Overlay, by defining rules for the content box, header, body and footer sections, and also specifies how markup should be hidden while the overlay is loading.
/* Hide overlay markup while loading, if js is enabled */ .yui3-js-enabled .yui3-overlay-loading { top: -1000em; left: -1000em; position: absolute; } /* Overlay Look/Feel */ .yui3-overlay-content { background-color: #ECEFFB; border: 1px solid #9EA8C6; border-radius: 3px; box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.25); } .yui3-overlay-content .yui3-widget-hd { background-color: #B6BFDA; color: #30418C; font-size: 120%; font-weight: bold; padding: 0.2em 0.5em 0.3em; border-radius: 2px 2px 0 0; } .yui3-overlay-content .yui3-widget-bd { padding: 0.4em 0.6em 0.5em; } .yui3-overlay-content .yui3-widget-ft { background-color:#DFE3F5; padding: 0.4em 0.6em 0.5em; border-radius: 0 0 2px 2px; }
Note: As discussed on the Widget user guide, all widgets are enclosed in 2 containing elements - the boundingBox is the outer(most) element, and the contentBox is the inner element into which the widget's content is added. It is advised to apply any look/feel CSS for the widget to the content box and its children. This leaves the bounding box without padding/borders, allowing for consistent positioning/sizing across box models.
Note: be sure to add the yui3-skin-sam
classname to the
page's <body>
element or to a parent element of the widget in order to apply
the default CSS skin. See Understanding Skinning.
<body class="yui3-skin-sam"> <!-- You need this skin class -->
<div class="overlay-example" id="overlay-position"> <label>X: <input type="text" id="x" value="0" ></label> <label>Y: <input type="text" id="y" value="0" ></label> <button type="button" id="move">Move</button> <button type="button" id="hide">Hide</button> <button type="button" id="show">Show</button> <div id="overlay" class="yui3-overlay-loading"> <div class="yui3-widget-hd">Overlay Header</div> <div class="yui3-widget-bd">Overlay Body</div> <div class="yui3-widget-ft">Overlay Footer</div> </div> <p class="filler"> <select class="needs-shim"> <option>Prevent IE6 Bleedthrough</option> </select> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nunc pretium quam eu mi varius pulvinar. Duis orci arcu, ullamcorper sit amet, luctus ut, interdum ac, quam. Pellentesque euismod. Nam tincidunt, purus in ultrices congue, urna neque posuere arcu, aliquam tristique purus sapien id nulla. Etiam rhoncus nulla at leo. Cras scelerisque nisl in nibh. Sed eget odio. Morbi elit elit, porta a, convallis sit amet, rhoncus non, felis. Mauris nulla pede, pretium eleifend, porttitor at, rutrum id, orci. Quisque non urna. Nulla aliquam rhoncus est. </p> </div> <script type="text/javascript"> YUI().use("overlay", function(Y) { var xy = Y.one("#overlay-position").getXY(); // Create an overlay from markup var overlay = new Y.Overlay({ srcNode:"#overlay", width:"13em", height:"10em", xy:[xy[0] + 30, xy[1] + 38] }); overlay.render(); var xInput = Y.one("#x"); var yInput = Y.one("#y"); // Using round to round sub-pixel values for FF3 just // to make the text box values prettier. xInput.set("value", Math.round(overlay.get("x"))); yInput.set("value", Math.round(overlay.get("y"))); Y.on("click", function(e) { var x = parseInt(xInput.get("value")); var y = parseInt(yInput.get("value")); overlay.move(x,y); }, "#move"); Y.on("click", Y.bind(overlay.show, overlay), "#show"); Y.on("click", Y.bind(overlay.hide, overlay), "#hide"); }); </script>