YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Container Family: Creating and Positioning an Overlay

# Container Family: Creating and Positioning an Overlay

Overlays are extensions of Modules and differ in the sense that the float above the normal page flow. They can be positioned in three different ways: By fixing them to the center of the viewport (overlay1 below), by specifying a position (overlay2), and by positioning them relative to a context element (overlay3).

Use the buttons in the example below to show and hide the three Overlay instances.

View example in new window.

overlay1:
overlay2:
overlay3:
Align to me
Overlay #1 from Markup
This is a Overlay that was marked up in the document.
End of Overlay #1

## Setting up Overlays

The Overlay Control is an extension of Module; its role is to facilitate the creation of modular content that is absolutely positioned above the flow of a page. It adds additional functionality to Module, including methods for positioning, multiple custom events for monitoring internal property changes, and a built-in `<iframe>` solution for dealing with `<select>` element bleed-through in Internet Explorer.

Overlay is fundamentally a building block for other UI controls. The concepts presented in this example will form the basis for the way that you interact with all of its subclasses, including Panel and Dialog.

In this tutorial we will build three Overlays with different types of positioning. One of them will be based on existing markup; the other two will be created dynamically using script. In addition to instantiating the Overlays, we will also use the constructor to pass configuration properties for each of our Overlays.

// Build overlay1 based on markup, initially hidden, fixed to the center of the viewport, and 300px wide YAHOO.example.container.overlay1 = new YAHOO.widget.Overlay("overlay1", { fixedcenter:true, visible:false, width:"300px" } ); YAHOO.example.container.overlay1.render(); // Build overlay2 dynamically, initially hidden, at position x:400,y:500, and 300px wide YAHOO.example.container.overlay2 = new YAHOO.widget.Overlay("overlay2", { xy:[400,500], visible:false, width:"300px" } ); YAHOO.example.container.overlay2.setHeader("Overlay #2 from Script"); YAHOO.example.container.overlay2.setBody("This is a dynamically generated Overlay."); YAHOO.example.container.overlay2.setFooter("End of Overlay #2"); YAHOO.example.container.overlay2.render(document.body); // Build overlay3 dynamically, initially hidden, aligned to context element "context", and 200px wide. // Re-align just before the overlay is shown and whenever the window is resized to account for changes in the position // of the context element after the initial context alignment YAHOO.example.container.overlay3 = new YAHOO.widget.Overlay("overlay3", { context:["ctx","tl","bl", ["beforeShow", "windowResize"]], visible:false, width:"200px" } ); YAHOO.example.container.overlay3.setHeader("Overlay #3 from Script"); YAHOO.example.container.overlay3.setBody("This is a dynamically generated Overlay."); YAHOO.example.container.overlay3.setFooter("End of Overlay #3"); YAHOO.example.container.overlay3.render(document.body); YAHOO.util.Event.addListener("show1", "click", YAHOO.example.container.overlay1.show, YAHOO.example.container.overlay1, true); YAHOO.util.Event.addListener("hide1", "click", YAHOO.example.container.overlay1.hide, YAHOO.example.container.overlay1, true); YAHOO.util.Event.addListener("show2", "click", YAHOO.example.container.overlay2.show, YAHOO.example.container.overlay2, true); YAHOO.util.Event.addListener("hide2", "click", YAHOO.example.container.overlay2.hide, YAHOO.example.container.overlay2, true); YAHOO.util.Event.addListener("show3", "click", YAHOO.example.container.overlay3.show, YAHOO.example.container.overlay3, true); YAHOO.util.Event.addListener("hide3", "click", YAHOO.example.container.overlay3.hide, YAHOO.example.container.overlay3, true);

These Overlays introduce a few of the configuration properties which are available to help position the Overlay.

- Centered In The Viewport

  The `fixedcenter` property, when set to true, will force the Overlay to always be positioned in the center of the viewport — even when the window is scrolled or resized. The `visible` property determines whether the Overlay should be visible, and the `width` property allows a CSS width to be set for the Overlay.

- At A Specific Page Co-ordinate

  Basic pixel-based positioning is available via the `xy` property, which can also be split into separate properties (`x` and `y`). The `xy` co-ordinates are page co-cordinates, relative to the top/left corner of the document.

- Aligned To An Existing Element On The Page

  The `context` property, as shown in `overlay3`, takes an array of values. The first entry in the array is the id of the element to which we want to anchor the Overlay. In this case, that element is a `<div>` with an id of `ctx`. The next two entries specify the positioning of the Overlay — `tl` and `bl` mean, "Anchor my Overlay's top left corner to my context element's bottom left corner." (Other possible values include `tr` and `br` for "top right" and "bottom right", respectively.). These first three entries in the array are required entries.

  The fourth entry in the array is optional and specifies the list of events (triggers) for which we want to re-align the Overlay with the context element. If this fourth "triggers" entry is not specified, context alignement is only done once, when the Overlay is created. This is sufficient for a majority of use cases, where the page co-ordinates of the context element remain constant. However in fluid layouts (such as this example template), the page co-ordinates of the context element change as the window is resized, therefore we ask the Overlay to align itself whenever the `"beforeShow"` and `"windowResize"` events are fired. The API documentation for the context configuration property discusses the events which are supported in more detail.

In the next step, we will define CSS styles that allow us to see a clear visual representation of our Overlays; remember, Overlays are building blocks for other controls and as such they are not styled by default. We will also style our `ctx` context element so that it's easy to see:

<style> .yui-overlay { border:1px dotted black;padding:5px;margin:10px; } .yui-overlay .hd { border:1px solid red;padding:5px; } .yui-overlay .bd { border:1px solid green;padding:5px; } .yui overlay .ft { border:1px solid blue;padding:5px; } #ctx { background:orange;width:100px;height:25px; } </style>

The markup for `overlay1`, plus the context element `ctx` and the buttons to show all our Overlays, is displayed below. Note that `overlay1` has an inline style of `visibility:hidden` set in advance. Most browsers are slower to render CSS than inline styles, and we want this marked-up Overlay to be hidden by default. If the inline style isn't present, it may cause a brief "flash of unstyled content" where the Overlay may be visible on slower machines.

<div> overlay1: <button id="show1">Show</button> <button id="hide1">Hide</button> </div> <div> overlay2: <button id="show2">Show</button> <button id="hide2">Hide</button> </div> <div> overlay3: <button id="show3">Show</button> <button id="hide3">Hide</button> </div> <div id="ctx">Align to me</div> <div id="overlay1" style="visibility:hidden"> <div class="hd">Overlay #1 from Markup</div> <div class="bd">This is a Overlay that was marked up in the document.</div> <div class="ft">End of Overlay #1</div> </div>

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### Container Family Examples:

- The Module Control
- Creating and Positioning an Overlay
- Simple Tooltip Example
- One Tooltip, Many Context Elements
- Simple Panel Example
- Skinning a Panel with Custom CSS: Introduction
- Skinning a Panel with Custom CSS: Advanced
- Creating a Modal "Loading" Panel
- Creating a Resizable Panel
- Dialog Quickstart Example
- SimpleDialog Quickstart Example
- Using ContainerEffect Transitions
- Using the Overlay Manager to Manage Multiple Panels
- Implementing Container Keyboard Shortcuts with KeyListener
- Using the Button ARIA Plugin (included with examples for Button Control)
- Using the Container ARIA Plugin
- Using A Menu Button To Replace A <select> Element (included with examples for Button Control)
- Replacing the content of a Button's Menu (included with examples for Button Control)
- Example of Color Picker Built in a Dialog via JavaScript (included with examples for Color Picker Control)
- Layout inside a resizable Panel (included with examples for Layout Manager)
- Editor in a Dialog Control (included with examples for Rich Text Editor)
- Menu Buttons (included with examples for Button Control)
- Popup Calendar - Advanced (included with examples for Calendar Control)
- Showing, Hiding, and Reordering Columns. (included with examples for DataTable Control)
- Split Buttons (included with examples for Button Control)
- Simple Calendar Menu Button (included with examples for Button Control)
- Calendar Menu Button with Date on Button Face (included with examples for Button Control)
- Color Picker Button (included with examples for Button Control)
- Fixed Width Menu Button (included with examples for Button Control)
- OS-Style Programs Menu (included with examples for Menu Family)
- Application Menubar (included with examples for Menu Family)
- Slider Button (included with examples for Button Control)
- Complex Application (included with examples for Layout Manager)

### More Container Family Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

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