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.
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.
1 | // Build overlay1 based on markup, initially hidden, fixed to the center of the viewport, and 300px wide |
2 | YAHOO.example.container.overlay1 = new YAHOO.widget.Overlay("overlay1", { fixedcenter:true, |
3 | visible:false, |
4 | width:"300px" } ); |
5 | YAHOO.example.container.overlay1.render(); |
6 | |
7 | |
8 | // Build overlay2 dynamically, initially hidden, at position x:400,y:500, and 300px wide |
9 | YAHOO.example.container.overlay2 = new YAHOO.widget.Overlay("overlay2", { xy:[400,500], |
10 | visible:false, |
11 | width:"300px" } ); |
12 | YAHOO.example.container.overlay2.setHeader("Overlay #2 from Script"); |
13 | YAHOO.example.container.overlay2.setBody("This is a dynamically generated Overlay."); |
14 | YAHOO.example.container.overlay2.setFooter("End of Overlay #2"); |
15 | YAHOO.example.container.overlay2.render(document.body); |
16 | |
17 | // Build overlay3 dynamically, initially hidden, aligned to context element "context", and 200px wide. |
18 | |
19 | // Re-align just before the overlay is shown and whenever the window is resized to account for changes in the position |
20 | // of the context element after the initial context alignment |
21 | YAHOO.example.container.overlay3 = new YAHOO.widget.Overlay("overlay3", { context:["ctx","tl","bl", ["beforeShow", "windowResize"]], |
22 | visible:false, |
23 | width:"200px" } ); |
24 | YAHOO.example.container.overlay3.setHeader("Overlay #3 from Script"); |
25 | YAHOO.example.container.overlay3.setBody("This is a dynamically generated Overlay."); |
26 | YAHOO.example.container.overlay3.setFooter("End of Overlay #3"); |
27 | YAHOO.example.container.overlay3.render(document.body); |
28 | |
29 | YAHOO.util.Event.addListener("show1", "click", YAHOO.example.container.overlay1.show, YAHOO.example.container.overlay1, true); |
30 | YAHOO.util.Event.addListener("hide1", "click", YAHOO.example.container.overlay1.hide, YAHOO.example.container.overlay1, true); |
31 | |
32 | YAHOO.util.Event.addListener("show2", "click", YAHOO.example.container.overlay2.show, YAHOO.example.container.overlay2, true); |
33 | YAHOO.util.Event.addListener("hide2", "click", YAHOO.example.container.overlay2.hide, YAHOO.example.container.overlay2, true); |
34 | |
35 | YAHOO.util.Event.addListener("show3", "click", YAHOO.example.container.overlay3.show, YAHOO.example.container.overlay3, true); |
36 | YAHOO.util.Event.addListener("hide3", "click", YAHOO.example.container.overlay3.hide, YAHOO.example.container.overlay3, true); |
view plain | print | ? |
These Overlays introduce a few of the configuration properties which are available to help position the Overlay.
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.
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.
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:
1 | <style> |
2 | .yui-overlay { border:1px dotted black;padding:5px;margin:10px; } |
3 | .yui-overlay .hd { border:1px solid red;padding:5px; } |
4 | .yui-overlay .bd { border:1px solid green;padding:5px; } |
5 | .yui overlay .ft { border:1px solid blue;padding:5px; } |
6 | |
7 | #ctx { background:orange;width:100px;height:25px; } |
8 | </style> |
view plain | print | ? |
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.
1 | <div> |
2 | overlay1: |
3 | <button id="show1">Show</button> |
4 | <button id="hide1">Hide</button> |
5 | </div> |
6 | <div> |
7 | overlay2: |
8 | <button id="show2">Show</button> |
9 | <button id="hide2">Hide</button> |
10 | </div> |
11 | <div> |
12 | overlay3: |
13 | <button id="show3">Show</button> |
14 | <button id="hide3">Hide</button> |
15 | </div> |
16 | |
17 | <div id="ctx">Align to me</div> |
18 | |
19 | <div id="overlay1" style="visibility:hidden"> |
20 | <div class="hd">Overlay #1 from Markup</div> |
21 | <div class="bd">This is a Overlay that was marked up in the document.</div> |
22 | <div class="ft">End of Overlay #1</div> |
23 | </div> |
view plain | print | ? |
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.
INFO 1819ms (+0) 12:56:29 AM:
Config
Firing Config event: iframe=false
IFRA 1819ms (+0) 12:56:29 AM:
global
xy: 8,501
INFO 1819ms (+0) 12:56:29 AM:
Config
Firing Config event: iframe=false
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: xy=8,501
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: y=501
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: x=8
INFO 1819ms (+0) 12:56:29 AM:
Config
Firing Config event: y=501
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: y=501
INFO 1819ms (+0) 12:56:29 AM:
Config
Firing Config event: iframe=false
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: xy=8,501
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: y=501
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: x=8
INFO 1819ms (+0) 12:56:29 AM:
Config
Firing Config event: x=8
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: x=8
INFO 1819ms (+0) 12:56:29 AM:
Config
Firing Config event: xy=8,501
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: xy=8,501
INFO 1819ms (+0) 12:56:29 AM:
Config
Firing Config event: context=[object HTMLDivElement],tl,bl,beforeShow,windowResize
INFO 1819ms (+0) 12:56:29 AM:
Config
Firing Config event: iframe=false
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: xy=8,501
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: y=501
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: x=8
INFO 1819ms (+0) 12:56:29 AM:
Config
Firing Config event: iframe=false
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: xy=600,200
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: y=200
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: x=600
INFO 1819ms (+0) 12:56:29 AM:
Config
Firing Config event: iframe=false
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: xy=484,298
INFO 1819ms (+0) 12:56:29 AM:
Config
setProperty: y=298
INFO 1819ms (+6) 12:56:29 AM:
Config
setProperty: x=484
INFO 1813ms (+0) 12:56:29 AM:
example
Overlay3 rendered.
INFO 1813ms (+0) 12:56:29 AM:
Config
Firing Config event: iframe=false
INFO 1813ms (+0) 12:56:29 AM:
Config
Config event queue: iframe=false,
INFO 1813ms (+0) 12:56:29 AM:
Config
queueProperty: iframe=undefined
INFO 1813ms (+0) 12:56:29 AM:
Config
setProperty: xy=8,501
INFO 1813ms (+0) 12:56:29 AM:
Config
setProperty: y=501
INFO 1813ms (+0) 12:56:29 AM:
Config
setProperty: x=8
INFO 1813ms (+0) 12:56:29 AM:
Config
Firing Config event: y=501
INFO 1813ms (+0) 12:56:29 AM:
Config
Config event queue: y=501, iframe=false,
INFO 1813ms (+0) 12:56:29 AM:
Config
queueProperty: iframe=undefined
INFO 1813ms (+0) 12:56:29 AM:
Config
setProperty: xy=8,501
INFO 1813ms (+0) 12:56:29 AM:
Config
setProperty: y=501
INFO 1813ms (+0) 12:56:29 AM:
Config
setProperty: x=8
INFO 1813ms (+0) 12:56:29 AM:
Config
Firing Config event: x=8
INFO 1813ms (+0) 12:56:29 AM:
Config
Config event queue: x=8, y=501, iframe=false,
INFO 1813ms (+0) 12:56:29 AM:
Config
queueProperty: iframe=undefined
IFRA 1813ms (+0) 12:56:29 AM:
global
xy: 8,501
INFO 1813ms (+0) 12:56:29 AM:
Config
Config event queue: x=8, y=501, iframe=false,
INFO 1813ms (+0) 12:56:29 AM:
Config
queueProperty: y=501
INFO 1813ms (+0) 12:56:29 AM:
Config
setProperty: y=501
INFO 1813ms (+0) 12:56:29 AM:
Config
Config event queue: x=8, iframe=false,
INFO 1813ms (+0) 12:56:29 AM:
Config
queueProperty: x=8
INFO 1813ms (+0) 12:56:29 AM:
Config
setProperty: x=8
INFO 1813ms (+0) 12:56:29 AM:
Config
Firing Config event: xy=8,501
INFO 1813ms (+0) 12:56:29 AM:
Config
Firing Config event: fixedcenter=false
INFO 1813ms (+0) 12:56:29 AM:
Config
Config event queue: fixedcenter=false, xy=8,501, iframe=false,
INFO 1813ms (+0) 12:56:29 AM:
Config
queueProperty: xy=8,501
INFO 1813ms (+1) 12:56:29 AM:
Config
setProperty: xy=8,501
INFO 1812ms (+0) 12:56:29 AM:
Config
setProperty: context=[object HTMLDivElement],tl,bl,beforeShow,windowResize
INFO 1812ms (+0) 12:56:29 AM:
Config
Firing Config event: context=ctx,tl,bl,beforeShow,windowResize
INFO 1812ms (+0) 12:56:29 AM:
Config
Config event queue: context=ctx,tl,bl,beforeShow,windowResize, fixedcenter=false, iframe=false,
INFO 1812ms (+0) 12:56:29 AM:
Config
queueProperty: iframe=undefined
INFO 1812ms (+0) 12:56:29 AM:
Config
Firing Config event: width=200px
INFO 1812ms (+0) 12:56:29 AM:
Config
Firing Config event: visible=false
INFO 1812ms (+0) 12:56:29 AM:
Config
Firing Config event: constraintoviewport=false
INFO 1812ms (+0) 12:56:29 AM:
Config
Firing Config event: preventcontextoverlap=false
INFO 1812ms (+0) 12:56:29 AM:
Config
setProperty: zindex=0
INFO 1812ms (+0) 12:56:29 AM:
Config
Firing Config event: zindex=null
INFO 1812ms (+1) 12:56:29 AM:
Config
setProperty: autofillheight=body
INFO 1811ms (+0) 12:56:29 AM:
Config
Firing Config event: autofillheight=body
INFO 1811ms (+0) 12:56:29 AM:
Config
Firing Config event: appendtodocumentbody=false
INFO 1811ms (+0) 12:56:29 AM:
Config
Firing Config event: monitorresize=true
INFO 1811ms (+0) 12:56:29 AM:
Config
Config event queue: monitorresize=true, appendtodocumentbody=false, autofillheight=body, zindex=null, preventcontextoverlap=false, constraintoviewport=false, visible=false, width=200px, context=ctx,tl,bl,beforeShow,windowResize, fixedcenter=false, iframe=false,
INFO 1811ms (+0) 12:56:29 AM:
Config
queueProperty: width=200px
INFO 1811ms (+0) 12:56:29 AM:
Config
Config event queue: monitorresize=true, appendtodocumentbody=false, fixedcenter=false, autofillheight=body, zindex=null, preventcontextoverlap=false, constraintoviewport=false, context=ctx,tl,bl,beforeShow,windowResize, iframe=false, visible=false,
INFO 1811ms (+0) 12:56:29 AM:
Config
queueProperty: visible=false
INFO 1811ms (+0) 12:56:29 AM:
Config
Config event queue: monitorresize=true, appendtodocumentbody=false, fixedcenter=false, visible=true, autofillheight=body, zindex=null, preventcontextoverlap=false, constraintoviewport=false, context=ctx,tl,bl,beforeShow,windowResize, iframe=false,
INFO 1811ms (+0) 12:56:29 AM:
Config
queueProperty: context=ctx,tl,bl,beforeShow,windowResize
INFO 1811ms (+0) 12:56:29 AM:
Config
Config event queue: monitorresize=true, appendtodocumentbody=false, fixedcenter=false, visible=true, autofillheight=body, iframe=false, zindex=null, preventcontextoverlap=false, constraintoviewport=false,
INFO 1811ms (+0) 12:56:29 AM:
Config
queueProperty: preventcontextoverlap=false
INFO 1811ms (+0) 12:56:29 AM:
Config
setProperty: preventcontextoverlap=false
INFO 1811ms (+0) 12:56:29 AM:
Config
Added property: preventcontextoverlap
INFO 1811ms (+0) 12:56:29 AM:
Config
Config event queue: monitorresize=true, appendtodocumentbody=false, fixedcenter=false, visible=true, autofillheight=body, constraintoviewport=false, iframe=false, zindex=null,
INFO 1811ms (+0) 12:56:29 AM:
Config
queueProperty: iframe=false
INFO 1811ms (+0) 12:56:29 AM:
Config
setProperty: iframe=false
INFO 1811ms (+0) 12:56:29 AM:
Config
Added property: iframe
INFO 1811ms (+0) 12:56:29 AM:
Config
Config event queue: monitorresize=true, appendtodocumentbody=false, fixedcenter=false, visible=true, autofillheight=body, zindex=null, constraintoviewport=false,
INFO 1811ms (+0) 12:56:29 AM:
Config
queueProperty: constraintoviewport=false
INFO 1811ms (+0) 12:56:29 AM:
Config
setProperty: constraintoviewport=false
INFO 1811ms (+0) 12:56:29 AM:
Config
Added property: constraintoviewport
INFO 1811ms (+0) 12:56:29 AM:
Config
Config event queue: monitorresize=true, appendtodocumentbody=false, fixedcenter=false, visible=true, autofillheight=body, zindex=null,
INFO 1811ms (+0) 12:56:29 AM:
Config
queueProperty: zindex=null
INFO 1811ms (+0) 12:56:29 AM:
Config
setProperty: zindex=null
INFO 1811ms (+0) 12:56:29 AM:
Config
Added property: zindex
INFO 1811ms (+0) 12:56:29 AM:
Config
Config event queue: monitorresize=true, appendtodocumentbody=false, fixedcenter=false, visible=true, autofillheight=body,
INFO 1811ms (+0) 12:56:29 AM:
Config
queueProperty: autofillheight=body
INFO 1811ms (+0) 12:56:29 AM:
Config
setProperty: autofillheight=body
INFO 1811ms (+0) 12:56:29 AM:
Config
Added property: autofillheight
INFO 1811ms (+0) 12:56:29 AM:
Config
setProperty: height=undefined
INFO 1811ms (+0) 12:56:29 AM:
Config
Added property: height
INFO 1811ms (+1811) 12:56:29 AM:
Config
setProperty: width=undefined
INFO 0ms (+0) 12:56:27 AM:
global
Logger initialized
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings