YUI 3.x Home -

YUI Library Examples: Overlay: Basic XY Positioning

Overlay: Basic XY Positioning

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.

Overlay Header
Overlay Body
Overlay Footer

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.

Basic XY Overlay Positioning

Setting Up The YUI Instance

To create an instance of an Overlay on you 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-ext and widget-stdmod dependencies it has.

  1. YUI({...}).use("overlay", function(Y) {
  2. // We'll write example code here, where we have a Y.Overlay class available.
  3. });
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.

Creating The Overlay From Markup

For this example, we'll create the overlay instance from markup which already exists on the page, and is shown below:

  1. <div id="overlay">
  2. <div class="yui-widget-hd">Overlay Header</div>
  3. <div class="yui-widget-bd">Overlay Body</div>
  4. <div class="yui-widget-ft">Overlay Footer</div>
  5. </div>
<div id="overlay">
    <div class="yui-widget-hd">Overlay Header</div>
    <div class="yui-widget-bd">Overlay Body</div>
    <div class="yui-widget-ft">Overlay Footer</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 yui-widget-hd, yui-widget-bd, yui-widget-ft classes to setup the Overlay's header, body and footer content attributes.

Instantiating The Overlay

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):

  1. // Create an overlay from markup, using an existing contentBox.
  2. var overlay = new Y.Overlay({
  3. contentBox:"#overlay",
  4. width:"10em",
  5. height:"10em",
  6. xy:[xy[0] + 10, xy[1] + 35]
  7. });
  8. overlay.render();
// Create an overlay from markup, using an existing contentBox.
var overlay = new Y.Overlay({
    xy:[xy[0] + 10, xy[1] + 35]

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.

Moving the Overlay

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.

  1. var xInput = Y.one("#x");
  2. var yInput = Y.one("#y");
  4. Y.on("click", function(e) {
  6. var x = parseInt(xInput.get("value"));
  7. var y = parseInt(yInput.get("value"));
  9. overlay.move(x,y);
  10. }, "#move");
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"));
}, "#move");

Overlay can be moved by invoking the move method, with either seperate 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).

CSS: Overlay Look/Feel

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 it's own look and feel for the Overlay, by defining rules for the content box, header, body and footer sections:

  1. /* Overlay Look/Feel */
  2. .yui-overlay-content {
  3. padding:3px;
  4. border:1px solid #000;
  5. background-color:#aaa;
  6. }
  8. .yui-overlay-content .yui-widget-hd {
  9. padding:5px;
  10. border:2px solid #aa0000;
  11. background-color:#fff;
  12. }
  14. .yui-overlay-content .yui-widget-bd {
  15. padding:5px;
  16. border:2px solid #0000aa;
  17. background-color:#fff;
  18. }
  20. .yui-overlay-content .yui-widget-ft {
  21. padding:5px;
  22. border:2px solid #00aa00;
  23. background-color:#fff;
  24. }
/* Overlay Look/Feel */
.yui-overlay-content {
    border:1px solid #000;
.yui-overlay-content .yui-widget-hd {
    border:2px solid #aa0000;
.yui-overlay-content .yui-widget-bd {
    border:2px solid #0000aa;
.yui-overlay-content .yui-widget-ft {
    border:2px solid #00aa00;

NOTE: As discussed on the Widget landing page, 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 it's children. This leaves the bounding box without padding/borders, allowing for consistent positioning/sizing across box models.

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings