YUI 3.x Home -

YUI Library Examples: Overlay: Extended XY Positioning

Overlay: Extended XY Positioning

This example shows how you can use Overlay's extended positioning support to align or center the overlay either in the viewport or relative to another node on the page. You can specify any one of 9 points (top-left, top-right, bottom-left, bottom-right, top-center, bottom-center, left-center, right-center, center) to align on both the Overlay and the node/viewport it is being aligned to.

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.

id = #align1
id = #align2
id = #align3

Extended XY Overlay Positioning - Align, Center Support

Setting Up The YUI Instance

As with the "Basic XY Positioning" example, 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-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.
});

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.

Instantiating The Overlay

For this example, we'll instantiate an Overlay, as we did for the "Basic XY Positioning" example, however we'll set the content for the Overlay sections using the headerContent and bodyContent attributes. We won't create a footer section for this overlay:

  1. /* Create Overlay from script, this time. With no footer */
  2. var overlay = new Y.Overlay({
  3. width:"10em",
  4. height:"10em",
  5. headerContent: "Aligned Overlay",
  6. bodyContent: "Click the 'Align Next' button to try a new alignment",
  7. zIndex:2
  8. });
  9.  
  10. /* Render it as a child of the #overlay-align element */
  11. overlay.render("#overlay-align");
/* Create Overlay from script, this time. With no footer */
var overlay = new Y.Overlay({
    width:"10em",
    height:"10em",
    headerContent: "Aligned Overlay",
    bodyContent: "Click the 'Align Next' button to try a new alignment",
    zIndex:2
});
 
/* Render it as a child of the #overlay-align element */
overlay.render("#overlay-align");

Since the Overlay is created from script, and doesn't currently exist in the document, we pass the overlay.render() method a selector query ("#overlay-align") for the node under which we want the Overlay to be rendered in the DOM. If we leave out this argument to render (or if the selector query doesn't bring back a node), the Overlay will be rendered to the current document's body element.

Aligning the overlay

The WidgetPositionExt extension used to create the overlay class adds the align and centered attributes to the Overlay, which can be used to align or center the Overlay relative to another element on the page (or the viewport).

The align attribute accepts as a value an object literal with the following properties:

node
The node to which the Widget is to be aligned. If set to null, or not provided, the Overlay is aligned to the viewport
points

A two element array, defining the two points on the Overlay and node which are to be aligned. The first element is the point on the Overlay, and the second element is the point on the node (or viewport). Supported alignment points are defined as static properties on the WidgetPositionExt extension.

e.g. [Y.WidgetPositionExt.TR, Y.WidgetPositionExt.TL] aligns the Top-Right corner of the Overlay with the Top-Left corner of the node/viewport, and [Y.WidgetPositionExt.CC, Y.WidgetPositionExt.TC] aligns the Center of the Overlay with the Top-Center edge of the node/viewport.

The centered property can either by set to true to center the Overlay in the viewport, or set to a selector string or node reference to center the Overlay in a particular node.

The example loops around a list of 6 alignment configurations, as the "Align Next" button is clicked:

  1. /* Setup local variable for Y.WidgetPositionExt, since we use it multiple times */
  2. var WidgetPositionExt = Y.WidgetPositionExt;
  3.  
  4. ...
  5.  
  6. /* Center in #overlay-align (example box) */
  7. overlay.set("align", {node:"#overlay-align",
  8. points:[WidgetPositionExt.CC, WidgetPositionExt.CC]});
  9.  
  10. /* Align top-left corner of overlay, with top-right corner of #align1 */
  11. overlay.set("align", {node:"#align1",
  12. points:[WidgetPositionExt.TL, WidgetPositionExt.TR]});
  13.  
  14. /* Center overlay in #align2 */
  15. overlay.set("centered", "#align2");
  16.  
  17. /* Align right-center edge of overlay with right-center edge of viewport */
  18. overlay.set("align", {points:[WidgetPositionExt.RC, WidgetPositionExt.RC]});
  19.  
  20. /* Center overlay in viewport */
  21. overlay.set("centered", true);
  22.  
  23. /* Align top-center edge of overlay with bottom-center edge of #align3 */
  24. overlay.set("align", {node:"#align3",
  25. points:[WidgetPositionExt.TC, WidgetPositionExt.BC]});
/* Setup local variable for Y.WidgetPositionExt, since we use it multiple times */
var WidgetPositionExt = Y.WidgetPositionExt;
 
...
 
/* Center in #overlay-align (example box) */
overlay.set("align", {node:"#overlay-align", 
                      points:[WidgetPositionExt.CC, WidgetPositionExt.CC]});
 
/* Align top-left corner of overlay, with top-right corner of #align1 */
overlay.set("align", {node:"#align1", 
                      points:[WidgetPositionExt.TL, WidgetPositionExt.TR]});
 
/* Center overlay in #align2 */
overlay.set("centered", "#align2");
 
/* Align right-center edge of overlay with right-center edge of viewport */
overlay.set("align", {points:[WidgetPositionExt.RC, WidgetPositionExt.RC]});
 
/* Center overlay in viewport */
overlay.set("centered", true);
 
/* Align top-center edge of overlay with bottom-center edge of #align3 */
overlay.set("align", {node:"#align3", 
                      points:[WidgetPositionExt.TC, WidgetPositionExt.BC]});

NOTE: Future verions will add support to re-align the Overlay in response to trigger events (e.g. window resize, scroll etc.) and support for constrained positioning.

CSS: Overlay Look/Feel

As mentioned in the "Basic XY Positioning" example, 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 and body sections:

  1. .yui-overlay-content {
  2. padding:2px;
  3. border:1px solid #000;
  4. background-color:#aaa;
  5. font-size:93%;
  6. }
  7.  
  8. .yui-overlay-content .yui-widget-hd {
  9. font-weight:bold;
  10. text-align:center;
  11. padding:2px;
  12. border:2px solid #aa0000;
  13. background-color:#fff;
  14. }
  15.  
  16. .yui-overlay-content .yui-widget-bd {
  17. text-align:left;
  18. padding:2px;
  19. border:2px solid #0000aa;
  20. background-color:#fff;
  21. }
.yui-overlay-content {
    padding:2px;
    border:1px solid #000;
    background-color:#aaa;
    font-size:93%;
}
 
.yui-overlay-content .yui-widget-hd {
    font-weight:bold;
    text-align:center;
    padding:2px;
    border:2px solid #aa0000;
    background-color:#fff;
}
 
.yui-overlay-content .yui-widget-bd {
    text-align:left;
    padding:2px;
    border:2px solid #0000aa;
    background-color:#fff;
}

Copyright © 2009 Yahoo! Inc. All rights reserved.

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