5
<title>Example: Basic XY Positioning</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: Basic XY Positioning</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><style type="text/css" scoped>
21
/* Hide overlay markup while loading, if js is enabled */
22
.yui3-js-enabled .yui3-overlay-loading {
28
/* Overlay Look/Feel */
29
.yui3-overlay-content {
31
border:1px solid #000;
32
background-color:#aaa;
35
.yui3-overlay-content .yui3-widget-hd {
37
border:2px solid #aa0000;
38
background-color:#fff;
41
.yui3-overlay-content .yui3-widget-bd {
43
border:2px solid #0000aa;
44
background-color:#fff;
47
.yui3-overlay-content .yui3-widget-ft {
49
border:2px solid #00aa00;
50
background-color:#fff;
53
/* Example Layout CSS */
60
.overlay-example button, .overlay-example label, .overlay-example select, .overlay-example input {
64
.overlay-example select.needs-shim {
68
.overlay-example .filler {
82
<p>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.</p>
83
<p>It also provides a couple of input fields, allowing you to invoke the Overlay's <code>move()</code> method, to move the Overlay to a specific XY position on the page.</p>
87
<div class="overlay-example" id="overlay-position">
89
<label>X: <input type="text" id="x" value="0" ></label>
90
<label>Y: <input type="text" id="y" value="0" ></label>
91
<button type="button" id="move">Move</button>
92
<button type="button" id="hide">Hide</button>
93
<button type="button" id="show">Show</button>
95
<div id="overlay" class="yui3-overlay-loading">
96
<div class="yui3-widget-hd">Overlay Header</div>
97
<div class="yui3-widget-bd">Overlay Body</div>
98
<div class="yui3-widget-ft">Overlay Footer</div>
102
<select class="needs-shim">
103
<option>Prevent IE6 Bleedthrough</option>
105
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.
109
<script type="text/javascript">
110
YUI().use("overlay", function(Y) {
112
var xy = Y.one("#overlay-position").getXY();
114
// Create an overlay from markup
115
var overlay = new Y.Overlay({
119
xy:[xy[0] + 10, xy[1] + 35]
123
var xInput = Y.one("#x");
124
var yInput = Y.one("#y");
126
// Using round to round sub-pixel values for FF3 just
127
// to make the text box values prettier.
128
xInput.set("value", Math.round(overlay.get("x")));
129
yInput.set("value", Math.round(overlay.get("y")));
131
Y.on("click", function(e) {
132
var x = parseInt(xInput.get("value"));
133
var y = parseInt(yInput.get("value"));
137
Y.on("click", Y.bind(overlay.show, overlay), "#show");
138
Y.on("click", Y.bind(overlay.hide, overlay), "#hide");
144
<h2>Basic XY Overlay Positioning</h2>
146
<h3>Setting Up The YUI Instance</h3>
148
<p>To create an instance of an Overlay on your page, the only module you need to request is the <code>overlay</code> module. The <code>overlay</code> module will pull in the <code>widget</code>, <code>widget-stack</code>, <code>widget-position</code>, <code>widget-position-align</code>, <code>widget-position-constrain</code> and <code>widget-stdmod</code> extensions it uses.</p>
150
<pre class="code prettyprint">YUI({...}).use("overlay", function(Y) {
151
// We'll write example code here, where we have a Y.Overlay class available.
155
<p>Note, using the <code>overlay</code> 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.</p>
157
<h3>Creating The Overlay From Markup</h3>
159
<p>For this example, we'll create the overlay instance from markup which already exists on the page, and is shown below. We mark the existing markup with the <code>yui3-overlay-loading</code> class, so that we can hide it while the rich control is being instantiated:</p>
161
<pre class="code prettyprint"><div id="overlay" class="yui3-overlay-loading">
162
<div class="yui3-widget-hd">Overlay Header</div>
163
<div class="yui3-widget-bd">Overlay Body</div>
164
<div class="yui3-widget-ft">Overlay Footer</div>
165
</div></pre>
168
<p>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 <code>yui3-widget-hd, yui3-widget-bd, yui3-widget-ft</code> classes to setup the Overlay's header, body and footer content attributes.</p>
170
<h3>Instantiating The Overlay</h3>
172
<p>To create an overlay instance, we use the overlay constructor <code>Y.Overlay</code>, and pass it the <code>contentBox</code> 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):</p>
174
<pre class="code prettyprint">// Create an overlay from markup, using an existing contentBox.
175
var overlay = new Y.Overlay({
176
srcNode:"#overlay",
177
width:"10em",
178
height:"10em",
179
xy:[xy[0] + 10, xy[1] + 35]
181
overlay.render();</pre>
184
<p>After creating the overlay instance, we invoke <code>overlay.render()</code> 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.</p>
186
<h3>Moving the Overlay</h3>
188
<p>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.</p>
190
<pre class="code prettyprint">var xInput = Y.one("#x");
191
var yInput = Y.one("#y");
193
Y.on("click", function(e) {
195
var x = parseInt(xInput.get("value"));
196
var y = parseInt(yInput.get("value"));
199
}, "#move");</pre>
202
<p>Overlay can be moved by invoking the <code>move</code> method, with either seperate x and y arguments (<code>move(100,200)</code>), or as an array (<code>move([100,200])</code>). The <code>x, y and xy</code> attributes can also be used to move the overlay, and are equivalent to the move method (<code>overlay.set("x", 200);overlay.set("xy", [100,200])</code>)</p>
204
<p>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).</p>
206
<h3>CSS: Overlay Look/Feel</h3>
208
<p>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.</p>
210
<p>The example provides it's own look and feel for the Overlay, by defining rules for the content box, header, body and footer sections, and also specifies how markup should be hidden while the overlay is loading.:</p>
212
<pre class="code prettyprint">/* Hide overlay markup while loading, if js is enabled */
213
.yui3-js-enabled .yui3-overlay-loading {
219
/* Overlay Look/Feel */
220
.yui3-overlay-content {
222
border:1px solid #000;
223
background-color:#aaa;
226
.yui3-overlay-content .yui3-widget-hd {
228
border:2px solid #aa0000;
229
background-color:#fff;
232
.yui3-overlay-content .yui3-widget-bd {
234
border:2px solid #0000aa;
235
background-color:#fff;
238
.yui3-overlay-content .yui3-widget-ft {
240
border:2px solid #00aa00;
241
background-color:#fff;
245
<p><strong>NOTE:</strong> As discussed on the <a href="../widget/index.html#widget-markup-diagram">Widget user guide</a>, 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.</p>
247
<h2>Complete Example Source</h2>
248
<pre class="code prettyprint"><div class="overlay-example" id="overlay-position">
250
<label>X: <input type="text" id="x" value="0" ></label>
251
<label>Y: <input type="text" id="y" value="0" ></label>
252
<button type="button" id="move">Move</button>
253
<button type="button" id="hide">Hide</button>
254
<button type="button" id="show">Show</button>
256
<div id="overlay" class="yui3-overlay-loading">
257
<div class="yui3-widget-hd">Overlay Header</div>
258
<div class="yui3-widget-bd">Overlay Body</div>
259
<div class="yui3-widget-ft">Overlay Footer</div>
262
<p class="filler">
263
<select class="needs-shim">
264
<option>Prevent IE6 Bleedthrough</option>
266
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.
270
<script type="text/javascript">
271
YUI().use("overlay", function(Y) {
273
var xy = Y.one("#overlay-position").getXY();
275
// Create an overlay from markup
276
var overlay = new Y.Overlay({
277
srcNode:"#overlay",
278
width:"10em",
279
height:"10em",
280
xy:[xy[0] + 10, xy[1] + 35]
284
var xInput = Y.one("#x");
285
var yInput = Y.one("#y");
287
// Using round to round sub-pixel values for FF3 just
288
// to make the text box values prettier.
289
xInput.set("value", Math.round(overlay.get("x")));
290
yInput.set("value", Math.round(overlay.get("y")));
292
Y.on("click", function(e) {
293
var x = parseInt(xInput.get("value"));
294
var y = parseInt(yInput.get("value"));
296
}, "#move");
298
Y.on("click", Y.bind(overlay.show, overlay), "#show");
299
Y.on("click", Y.bind(overlay.hide, overlay), "#hide");
301
</script></pre>
306
<div id="sidebar" class="yui3-u">
310
<div class="sidebox">
312
<h2 class="no-toc">Examples</h2>
316
<ul class="examples">
319
<li data-description="Shows how to instantiate a basic Overlay instance, and use the Overlay's basic XY positioning support.">
320
<a href="overlay-xy.html">Basic XY Positioning</a>
325
<li data-description="Shows how to use the Overlay's XY alignment support, to align the Overlay relative to another element, or to the viewport.">
326
<a href="overlay-align.html">Alignment Support</a>
331
<li data-description="Shows how to use the Overlay's zindex and shim support when positioning Overlays above other elements on the page.">
332
<a href="overlay-stack.html">Stack Support</a>
337
<li data-description="Shows how to modify content in the Overlay's header, body and footer sections.">
338
<a href="overlay-stdmod.html">Standard Module Support</a>
343
<li data-description="Shows how to use Overlay's constrainment support, to limit the XY value which can be set for an Overlay.">
344
<a href="overlay-constrain.html">Constrain Support</a>
349
<li data-description="Shows how to create a simple plugin to retrieve content for the Overlay using the io utility.">
350
<a href="overlay-io-plugin.html">IO Plugin</a>
355
<li data-description="Shows how to create a simple plugin to animate the Overlay's movement and visibility.">
356
<a href="overlay-anim-plugin.html">Animation Plugin</a>
370
<div class="sidebox">
372
<h2 class="no-toc">Examples That Use This Component</h2>
376
<ul class="examples">
393
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States.">
394
<a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
399
<li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
400
<a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
412
<script src="../assets/vendor/prettify/prettify-min.js"></script>
413
<script>prettyPrint();</script>