YUI recommends YUI3.
YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.
This documentation is no longer maintained.
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.
Note: Overlay is a building block for other UI controls and does not in and of itself solve a particular interaction problem; if you're looking for a specific implementation of Overlay, check out the Tooltip, Panel, Dialog and SimpleDialog controls.
To use Overlay, include the following code in your page:
<!-- Sam Skin CSS --> <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.9.0/build/container/assets/skins/sam/container.css"> <!-- Dependencies --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script> <!-- OPTIONAL: Animation (only required if using ContainerEffect) --> <script src="http://yui.yahooapis.com/2.9.0/build/animation/animation-min.js"></script> <!-- Source file --> <script src="http://yui.yahooapis.com/2.9.0/build/container/container-min.js"></script>
<!-- Sam Skin CSS --> <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.9.0/build/container/assets/skins/sam/container.css"> <!-- Dependencies --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script> <!-- OPTIONAL: Animation (only required if using ContainerEffect) --> <script src="http://yui.yahooapis.com/2.9.0/build/animation/animation-min.js"></script> <!-- Source file --> <script src="http://yui.yahooapis.com/2.9.0/build/container/container-min.js"></script>
If you are not using Tooltip, Panel, Dialog, or SimpleDialog, you may substitute container-min.js
with the smaller container_core-min.js
file:
<!-- If not using Tooltip, Panel, Dialog, or SimpleDialog: --> <script src="http://yui.yahooapis.com/2.9.0/build/container/container_core-min.js"></script>
<!-- If not using Tooltip, Panel, Dialog, or SimpleDialog: --> <script src="http://yui.yahooapis.com/2.9.0/build/container/container_core-min.js"></script>
Note that a CSS file is not required if you're only using container_core-min.js
, however for the Overlay to operate as designed,
it does need to be positioned absolutely. The yui-overlay
class applied to all overlays can be used to define your own rule to apply
position:absolute
for all overlays. Alternatively you could include the container.css
file mentioned above, but since it only has a single
rule which applies to Overlays, it's overkill if you're aiming to optimize the amount of code required for your page.
yui-skin-sam
class name to an element that is a parent of the element
in which the Overlay Control lives. You can usually accomplish this simply by putting the class on the
<body>
tag:
<body class="yui-skin-sam">
<body class="yui-skin-sam">
For more information on skinning YUI components and making use of default skins, see our Understanding YUI Skins article here on the website.
Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader to write out the full HTML for including the precise files you need for your implementation.
Note: If you wish to include this component via the YUI Loader, its module name is container
. (Click here for the full list of module names for YUI Loader.)
Where these files come from: The files included using the text above will be served from Yahoo! servers. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug
versions of YUI JavaScript files, please download the library distribution and host the files on your own server.
Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.
This section describes common tasks for creating and using Overlay. It contains these sections:
Note that Overlay inherits its constructor and configuration, as well as several other important methods, from Module. See Module for more information on how to utilize Module's features.
In its most basic form, the HTML markup for an Overlay looks like this:
<div id="myOverlay"> <div class="hd"></div> <div class="bd"></div> <div class="ft"></div> </div>
<div id="myOverlay"> <div class="hd"></div> <div class="bd"></div> <div class="ft"></div> </div>
To instantiate an Overlay around a pre-existing piece of markup (as exemplified above), simply pass the id of your modular content to the Overlay constructor:
var myOverlay = new YAHOO.widget.Overlay("myOverlay");
var myOverlay = new YAHOO.widget.Overlay("myOverlay");
Overlay has several useful methods that can be used to position the Overlay, by either specifying specific pixel coordinates, or using special methods for contextually aligning or centering the Overlay.
To position an Overlay using pixel coordinates, use the x
, y
, or xy
properties:
// Set the x position myOverlay.cfg.setProperty("x", 100); // Set the y positon myOverlay.cfg.setProperty("y", 200); // Set the x and y positions simultaneously myOverlay.cfg.setProperty("xy", [100,200]);
// Set the x position myOverlay.cfg.setProperty("x", 100); // Set the y positon myOverlay.cfg.setProperty("y", 200); // Set the x and y positions simultaneously myOverlay.cfg.setProperty("xy", [100,200]);
To center an Overlay manually, you can call its center()
method:
// Center the Overlay myOverlay.center();
// Center the Overlay myOverlay.center();
An alternative to manually centering the Overlay is to set its fixedcenter
property to always keep the Overlay centered in the viewport:
// Center the Overlay myOverlay.cfg.setProperty("fixedcenter", true);
// Center the Overlay myOverlay.cfg.setProperty("fixedcenter", true);
To align an Overlay to another element on the page contextually, you can set the context
property:
// This call says that myOverlay's top right corner should be snapped to "myContextEl"'s top left corner. myOverlay.cfg.setProperty("context", ["myContextEl", "tr", "tl"]); // To change the alignment later, you can also call "align" once the context property has already been set. // This call will change the alignment so that myOverlay's top right corner is snapped to "myContextEl"'s bottom left corner. myOverlay.align("tr", "bl");
// This call says that myOverlay's top right corner should be snapped to "myContextEl"'s top left corner. myOverlay.cfg.setProperty("context", ["myContextEl", "tr", "tl"]); // To change the alignment later, you can also call "align" once the context property has already been set. // This call will change the alignment so that myOverlay's top right corner is snapped to "myContextEl"'s bottom left corner. myOverlay.align("tr", "bl");
Context based alignment by default is a one time operation - the Overlay will be aligned to the context element once, when the property is set (or, if the developer calls Overlay's align()
method as shown above).
For example, unlike the fixedcenter
property which repositions the Overlay when the window is resized or scrolled, context
does not realign the Overlay with the context element in response to windowScroll
or windowResize
events. This is because in most use cases,
the page co-ordinates of the context element remains unchanged when the user scrolls or resizes the window.
However if the layout of your page could result in the page co-orindates of the context element being modified (e.g. in fluid layouts), you can provide an optional list of events which should trigger context element re-alignment as shown below:
// myOverlay's top right corner should be snapped to "myContextEl"'s top left corner, and we should realign just before the Overlay is shown, or whenever the window is resized: myOverlay.cfg.setProperty("context", ["myContextEl", "tr", "tl", ["beforeShow", "windowResize"]]);
// myOverlay's top right corner should be snapped to "myContextEl"'s top left corner, and we should realign just before the Overlay is shown, or whenever the window is resized: myOverlay.cfg.setProperty("context", ["myContextEl", "tr", "tl", ["beforeShow", "windowResize"]]);
Once initialized, an Overlay is rendered in the same way as Module, by invoking the
render()
method. As with Module, if the Overlay was not created from markup you need to pass to the render
method, the parent element to which the Overlay should be added.
One piece of core functionality provided by Overlay is support for a zIndex
configuration property that enables it to be stacked above other elements on the page.
The zIndex configuration property is an interface for setting the
CSS z-index property, and
understanding how the browser handles z-index values is therefore an important part of using the
Overlay control and its subclasses.
A commonly asked question when working with Overlays is "Why is my Overlay rendered under DIV X, even though DIV X has a z-index of 10, and I've set the z-index of my Overlay to 9999?". Understanding stacking contexts will help you answer this question.
Relatively and absolutely positioned elements can overlap other elements on the page. In situations where elements overlap, the browser uses the CSS z-index property to determine how overlapping elements are stacked on top of each other along the z-axis: Generally speaking elements with higher z-indices are rendered above elements with lower z-indices.
Consider the following simple markup structure:
<body> <!-- DIV A and B are statically positioned, DIV A1, B1 are absolutely positioned --> <div id="A"> <div id="A1" style="position:absolute;z-index:20;">A1, z-index = 20</div> </div> <div id="B"> <div id="B1" style="position:absolute;z-index:10;">B1, z-index = 10</div> </div> </body>
<body> <!-- DIV A and B are statically positioned, DIV A1, B1 are absolutely positioned --> <div id="A"> <div id="A1" style="position:absolute;z-index:20;">A1, z-index = 20</div> </div> <div id="B"> <div id="B1" style="position:absolute;z-index:10;">B1, z-index = 10</div> </div> </body>
In the example above, DIV A1
would be rendered on top of DIV B1
if they were to overlap. DIV A1
and DIV B1
both have the same
stacking context, the root or body
element, and are stacked relative to
each other within this context based on their z-index values.
However, if DIV A
and DIV B
are either relatively or
absolutely positioned, the scenario changes:
<body> <!-- DIV A and B are now relatively positioned --> <div id="A" style="position:relative; z-index:1"> <div id="A1" style="position:absolute;z-index:20;">A1, z-index = 20</div> </div> <div id="B" style="position:relative; z-index:2"> <div id="B1" style="position:absolute;z-index:10;">B1, z-index = 10</div> </div> </body>
<body> <!-- DIV A and B are now relatively positioned --> <div id="A" style="position:relative; z-index:1"> <div id="A1" style="position:absolute;z-index:20;">A1, z-index = 20</div> </div> <div id="B" style="position:relative; z-index:2"> <div id="B1" style="position:absolute;z-index:10;">B1, z-index = 10</div> </div> </body>
DIV B1 (z-index=10)
will be rendered on top of DIV A1 (z-index=20)
, even though it has a lower z-index.
With different, positioned, parent elements, DIV A1
and DIV B1
no
longer share a stacking context. As per the
W3C layering spec,
DIV A
and DIV B
establish stacking contexts since they are relatively
positioned AND have a z-index defined. Therefore, when determining how to
stack the elements the browser doesn't compare their z-index values relative to each other, but
rather to other elements in the stacking contexts established by their separate parent elements.
Additionally, since DIV A
and DIV B
share a stacking context (body
),
they are stacked so that DIV B (z-index=2)
is placed above DIV A (z-index=1)
,
based on their z-index values. Furthermore, any elements which are children of DIV A
will always be below DIV B
and any of its children. No matter how high or low
the z-indices of DIV A1
and DIV B1
are set, DIV B1
will
always stack above DIV A1
, because DIV B
is stacked above DIV A
.
As mentioned above, positioned elements with a specified z-index will establish new stacking contexts for their child nodes. This is true for all A-Grade browsers. However in IE even positioned elements that lack a defined z-index establish a new stacking context. This is considered to be an IE bug.
To completely avoid the potential impact of ancestor elements establishing new stacking contexts,
render Overlays into the document's <body>
element. Doing so will gaurantee
that all Overlay instances exist within the same stacking context, making it easier to manage how
they are stacked above all other elements in the page.
Overlay has the following configuration properties, which it passes on to all of its subclasses through inheritance:
Name | Type | Default | Description |
---|---|---|---|
x | Number | null | Sets the element's page X co-ordinate. |
y | Number | null | Sets the element's page Y co-ordinate. |
xy | Array | null | Sets the element's page XY co-ordinates. |
context | Array | null |
Allows the Overlay to be aligned relative to a context element. The property expects an array value with the format: [contextElementOrId, overlayCorner, contextElementCorner] ,
where "contextElementOrId" is the context element or the id of the context element.
The corner parameters are one of the following string values: "tr" (top right), "tl" (top left), "br" (bottom right), or "bl" (bottom left) and define which corners of the overlay and context element should be aligned. The array also supports optional 4th and 5th entries. The 4th entry is an optional array of event names, or Custom Event objects, which should trigger re-alignment of the Overlay with the currently configured context element. For example:
Will re-align the Overlay to the context element just before it's shown, and whenever the window is resized. The 5th entry is an optional XY pixel offset, which is to be applied after the Overlay is aligned to the specified corner of the context element, and can be used to add a pixel buffer between the context element and the Overlay. For example:
Will offset the Overlay by 10 pixels along the X axis, and 20 pixels along the Y axis, after aligning the specified corners. |
effect | Object / Object[] | none | Sets the ContainerEffect (one or many) that should be used when showing and hiding the Overlay. |
fixedcenter | Boolean / String | false | Specifies whether the Overlay should be automatically centered in the viewport on window scroll and resize. It also supports the string value |
width | String | null | Sets the element's "width" style property. |
height | String | null | Sets the element's "height" style property. |
zIndex | Number | null | Sets the element's "z-index" style property. |
constraintoviewport | Boolean | false | If set to true the Overlay will try to remain inside the confines of the size of viewport. |
iframe | Boolean | false (true by default for IE 6 and below) | If set to true the Overlay will have and iframe behind it to prevent other elements with a higher z-index from poking through. |
autofillheight | String | "body" | Which container element (header, body or footer) should be sized to fill out any remaining
vertical space when a height is set on the container using the height configuration property. Supported values are "header", "body" and "footer". Can be set to null (or false) to turn off the feature.
|
Overlay inherits the following properties:
Name | Type | Default | Description |
---|---|---|---|
visible | Boolean | true | Sets whether or not the Overlay is visible on the page (Overlay uses the CSS "visibility" property to control this). |
monitorresize | Boolean | true | Configures whether or not to create a hidden off-screen element that can be used to monitor for text size changes in the DOM. |
The YUI Library and related topics are discussed on the on the YUILibrary.com forums.
Also be sure to check out YUIBlog for updates and articles about the YUI Library written by the library's developers.
The YUI Library's public bug tracking and feature request repositories are located on the YUILibrary.com site. Before filing new feature requests or bug reports, please review our reporting guidelines.
be the first to bookmark this page!
All YUI 2.x users should review the YUI 2.8.2 security bulletin, which discusses a vulnerability present in YUI 2.4.0-2.8.1.
Copyright © 2013 Yahoo! Inc. All rights reserved.