<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example: Alignment Support</title>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
<link rel="stylesheet" href="../assets/css/main.css">
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
<script src="../../build/yui/yui-min.js"></script>
</head>
<body>
<!--
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
-->
<div id="doc">
<div id="hd">
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
</div>
<h1>Example: Alignment Support</h1>
<div class="yui3-g">
<div class="yui3-u-3-4">
<div id="main">
<div class="content"><style type="text/css" scoped>
/* Overlay Look/Feel */
.yui3-overlay-content {
background-color: #ECEFFB;
border: 1px solid #9EA8C6;
border-radius: 3px;
box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.25);
}
.yui3-overlay-content .yui3-widget-hd {
background-color: #B6BFDA;
color: #30418C;
font-size: 120%;
font-weight: bold;
padding: 0.2em 0.5em 0.3em;
border-radius: 2px 2px 0 0;
}
.yui3-overlay-content .yui3-widget-bd {
padding: 0.4em 0.6em 0.5em;
}
.yui3-overlay-content .yui3-widget-ft {
background-color:#DFE3F5;
padding: 0.4em 0.6em 0.5em;
border-radius: 0 0 2px 2px;
}
/* Example Layout CSS */
.example .overlay-example {
position: relative;
padding: 5px;
height: 34.5em;
}
.example #overlay-align {
border: solid 1px #fff;
}
.example .overlay-example button {
margin-left: 1px;
}
.example .overlay-example .yui3-g {
color: #999;
}
.example .align-box {
background-color: #ddd;
border: solid 1px #ddd;
margin: 0px;
text-align: center;
width: 100%;
}
.example .overlay-example .content {
margin: 0 1em 0 0;
padding: 0;
}
.example .overlay-example .example-head {
border-bottom: solid 1px #ddd;
margin-bottom: 2em;
padding-bottom: 1em;
height: 8.6em;
}
.example .overlay-example .example-head pre{
margin: 0;
}
.example #alignment {
position: absolute;
right: 5px;
text-align: left;
top: 0;
height: 9em;
width: 36em;
}
.example .nav{
list-style-type: none;
-moz-padding-start: 0;
-webkit-padding-start: 0;
padding-start: 0;
margin: 0;
padding: 0;
}
.example .nav li {
border: solid 1px #ddd;
width: 72px;
height: 2em;
line-height: 2em;
text-indent: 0.3em;
}
.example .align-box .title {
color: #000;
padding: 2px;
}
.example #align2 {
width: 383px;
height: 164px;
}
.example #align3 {
height: 3em;
line-height: 3em;
}
.example #step {
font-size: 55%;
margin-left: 22px;
font-weight: normal;
}
#main .aligned-to{ /* highlight the node that overlay is currently aligned to */
border: solid 1px #f00;
}
</style>
<div class="intro">
<p>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.</p>
</div>
<div class="example">
<div class="overlay-example" id="overlay-align">
<div class="example-head">
<button type="button" class="button" id="align">View Next Alignment</button>
<pre class="code" id="alignment"></pre>
</div>
<div class="yui3-g">
<div class="yui3-u-1-8">
<ul class="nav">
<li>Nibh</li>
<li id="align1" class="align-box"><span class="title">#align1</span></li>
<li>Arcu</li>
<li>Congue</li>
<li>Purus</li>
<li>Quam</li>
<li>Neque</li>
<li>Aiquam</li>
<li>Eget</li>
<li>Etiam</li>
</ul>
</div>
<div class="yui3-u-5-8">
<div class="content">
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
<img id="align2" class="align-box" src="../assets/overlay/img/nevada.png" alt="Nevada landscape (id='align2')"/>
congue, urna neque posuere arcu, aliquam tristique purus sapien id nulla.
Etiam rhoncus nulla at leo. Cras scelerisque nisl in nibh. Sed eget odio.
</div>
</div>
<div class="yui3-u-1-4">
<div class="content">
<div id="align3" class="align-box"><span class="title">#align3</span></div>
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.
</div>
</div>
</div>
</div>
<script type="text/javascript">
YUI().use("overlay", function(Y) {
/* Create Overlay from script, this time. No footer */
var overlay = new Y.Overlay({
width:"16em",
headerContent: 'Aligned Overlay<span id="step"></span>',
bodyContent: '<div class="descr"></div>',
zIndex:2
});
/* Render it to #overlay-align element */
overlay.render("#overlay-align");
var alignment = Y.one("#alignment");
var stepNumber = Y.one("#step");
var descr = Y.one('.yui3-widget-bd .descr');
/* Setup local variable for Y.WidgetPositionAlign, since we use it multiple times */
var a = Y.WidgetPositionAlign,
declareA = "var a = Y.WidgetPositionAlign; // Local variable<br><br>",
dontDeclareA = "<br><br>";
var steps = [
function() {
/* Align top-left corner of overlay, with top-right corner of #align1 */
alignment.setHTML(declareA + 'overlay.set("align", {<br> node: "#align1",<br> points: [a.TL, a.TR]<br>});');
descr.setHTML('Align top-left corner of overlay, with top-right corner of #align1');
Y.one('#align1').addClass('aligned-to');
overlay.set("align", {node:"#align1", points: [a.TL, a.TR]});
},
function() {
/* Center overlay in #align2 */
alignment.setHTML(dontDeclareA + 'overlay.set("centered", "#align2");');
descr.setHTML('Center overlay in #align2 <br>(an image)');
Y.one('#align2').addClass('aligned-to');
overlay.set("centered", "#align2");
},
function() {
/* Align top-center edge of overlay with bottom-center edge of #align3 */
alignment.setHTML(declareA + 'overlay.set("align", {<br> node: "#align3",<br> points: [a.TC, a.BC]<br>});');
descr.setHTML('Align top-center edge of overlay with bottom-center edge of #align3');
Y.one('#align3').addClass('aligned-to');
overlay.set("align", {node:"#align3", points:[a.TC, a.BC]});
},
function() {
/* Align right-center edge of overlay with right-center edge of viewport */
alignment.setHTML(declareA + 'overlay.set("align", {<br> points: [a.RC, a.RC]<br>});');
descr.setHTML('Align right-center edge of overlay with right-center edge of viewport');
overlay.set("align", {points:[a.RC, a.RC]});
},
function() {
/* Center overlay in viewport */
alignment.setHTML(dontDeclareA + 'overlay.set("centered", true);');
descr.setHTML('Center overlay in viewport');
overlay.set("centered", true);
},
function() {
/* Center in #overlay-align */
alignment.setHTML(declareA + 'overlay.set("align", {<br> node: "#overlay-align",<br> points: [a.CC, a.CC]<br>});');
descr.setHTML('Center in #overlay-align');
Y.one('#overlay-align').addClass('aligned-to');
overlay.set("align", {node:"#overlay-align", points:[a.CC, a.CC]});
}
];
var step = 0;
var totalSteps = steps.length;
function alignNext() {
stepNumber.setHTML((step+1) + " of " + totalSteps);
Y.all('.aligned-to').removeClass('aligned-to');
steps[step]();
step = (++step)%(totalSteps);
}
alignNext();
Y.on("click", alignNext, "#align");
});
</script>
</div>
<h2>Aligned XY Overlay Positioning - align and center Support</h2>
<h3>Setting Up The YUI Instance</h3>
<p>As with the <a href="overlay-xy.html">"Basic XY Positioning"</a> example, 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>
<pre class="code prettyprint">YUI({...}).use("overlay", function(Y) {
// We'll write example code here, where we have a Y.Overlay class available.
});</pre>
<p>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>
<p>
<strong>Note:</strong> be sure to add the <code>yui3-skin-sam</code> classname to the
page's <code><body></code> element or to a parent element of the widget in order to apply
the default CSS skin. See <a href="http://yuilibrary.com/yui/docs/tutorials/skins/">Understanding Skinning</a>.
</p>
<pre class="code prettyprint"><body class="yui3-skin-sam"> <!-- You need this skin class --></pre>
<h3>Instantiating The Overlay</h3>
<p>For this example, we'll instantiate an Overlay, as we did for the <a href="overlay-xy.html">"Basic XY Positioning"</a> example, however we'll set the content for the Overlay sections using the <code>headerContent</code> and <code>bodyContent</code> attributes. We won't create a footer section for this overlay:</p>
<pre class="code prettyprint">/* Create Overlay from script, this time. With no footer */
var overlay = new Y.Overlay({
width:"16em",
headerContent: 'Aligned Overlay<span id="step"></span>',
bodyContent: '<div class="descr"></div><div id="alignment"></div>',
zIndex:2
});
/* Render it as a child of the #overlay-align element */
overlay.render("#overlay-align");</pre>
<p>Since the Overlay is created from script, and doesn't currently exist in the document, we pass the <code>overlay.render()</code> method a selector query (<code>"#overlay-align"</code>) 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.</p>
<h3>Aligning the overlay</h3>
<p>The <a href="http://yuilibrary.com/yui/docs/api/WidgetPositionAlign.html"><code>WidgetPositionAlign</code></a> extension used to create the overlay class adds the <code>align</code> and <code>centered</code> attributes to the Overlay, which can be used to align or center the Overlay relative to another element on the page (or the viewport).</p>
<p>The <code>align</code> attribute accepts as a value an object literal with the following properties:</p>
<dl>
<dt>node</dt>
<dd>
The node to which the Widget is to be aligned. If set to null, or not provided, the Overlay is aligned to the viewport
</dd>
<dt>points</dt>
<dd>
<p>
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 <code>WidgetPositionAlign</code> extension.
</p>
<p>
e.g. <code>[Y.WidgetPositionAlign.TR, Y.WidgetPositionAlign.TL]</code> aligns the Top-Right corner of the Overlay with the
Top-Left corner of the node/viewport, and <code>[Y.WidgetPositionAlign.CC, Y.WidgetPositionAlign.TC]</code> aligns the Center of the
Overlay with the Top-Center edge of the node/viewport.
</p>
</dd>
</dl>
<p>The <code>centered</code> property can either by set to <code>true</code> to center the Overlay in the viewport, or set to a selector string or node reference to center the Overlay in a particular node.</p>
<p>The example loops around a list of 6 alignment configurations, as the "Align Next" button is clicked:</p>
<pre class="code prettyprint">/* Setup local variable for Y.WidgetPositionAlign, since we use it multiple times */
var a = Y.WidgetPositionAlign;
...
/* Align top-left corner of overlay, with top-right corner of #align1 */
overlay.set("align", {node:"#align1",
points:[a.TL, a.TR]});
/* Center overlay in #align2 */
overlay.set("centered", "#align2");
/* Align top-center edge of overlay with bottom-center edge of #align3 */
overlay.set("align", {node:"#align3",
points:[a.TC, a.BC]});
/* Align right-center edge of overlay with right-center edge of viewport */
overlay.set("align", {points:[a.RC, a.RC]});
/* Center overlay in viewport */
overlay.set("centered", true);
/* Center in #overlay-align (example box) */
overlay.set("align", {node:"#overlay-align",
points:[a.CC, a.CC]});</pre>
<h3>CSS: Overlay Look/Feel</h3>
<p>As mentioned in the <a href="overlay-xy.html">"Basic XY Positioning"</a> 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.</p>
<p>The example provides it's own look and feel for the Overlay, by defining rules for the content box, header and body sections:</p>
<pre class="code prettyprint">/* Overlay Look/Feel */
.yui3-overlay-content {
background-color: #ECEFFB;
border: 1px solid #9EA8C6;
border-radius: 3px;
box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.25);
}
.yui3-overlay-content .yui3-widget-hd {
background-color: #B6BFDA;
color: #30418C;
font-size: 120%;
font-weight: bold;
padding: 0.2em 0.5em 0.3em;
border-radius: 2px 2px 0 0;
}
.yui3-overlay-content .yui3-widget-bd {
padding: 0.4em 0.6em 0.5em;
}
.yui3-overlay-content .yui3-widget-ft {
background-color:#DFE3F5;
padding: 0.4em 0.6em 0.5em;
border-radius: 0 0 2px 2px;
}</pre>
<h2>Complete Example Source</h2>
<pre class="code prettyprint"><div class="overlay-example" id="overlay-align">
<div class="example-head">
<button type="button" class="button" id="align">View Next Alignment</button>
<pre class="code" id="alignment"></pre>
</div>
<div class="yui3-g">
<div class="yui3-u-1-8">
<ul class="nav">
<li>Nibh</li>
<li id="align1" class="align-box"><span class="title">#align1</span></li>
<li>Arcu</li>
<li>Congue</li>
<li>Purus</li>
<li>Quam</li>
<li>Neque</li>
<li>Aiquam</li>
<li>Eget</li>
<li>Etiam</li>
</ul>
</div>
<div class="yui3-u-5-8">
<div class="content">
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
<img id="align2" class="align-box" src="../assets/overlay/img/nevada.png" alt="Nevada landscape (id='align2')"/>
congue, urna neque posuere arcu, aliquam tristique purus sapien id nulla.
Etiam rhoncus nulla at leo. Cras scelerisque nisl in nibh. Sed eget odio.
</div>
</div>
<div class="yui3-u-1-4">
<div class="content">
<div id="align3" class="align-box"><span class="title">#align3</span></div>
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.
</div>
</div>
</div>
</div>
<script type="text/javascript">
YUI().use("overlay", function(Y) {
/* Create Overlay from script, this time. No footer */
var overlay = new Y.Overlay({
width:"16em",
headerContent: 'Aligned Overlay<span id="step"></span>',
bodyContent: '<div class="descr"></div>',
zIndex:2
});
/* Render it to #overlay-align element */
overlay.render("#overlay-align");
var alignment = Y.one("#alignment");
var stepNumber = Y.one("#step");
var descr = Y.one('.yui3-widget-bd .descr');
/* Setup local variable for Y.WidgetPositionAlign, since we use it multiple times */
var a = Y.WidgetPositionAlign,
declareA = "var a = Y.WidgetPositionAlign; // Local variable<br><br>",
dontDeclareA = "<br><br>";
var steps = [
function() {
/* Align top-left corner of overlay, with top-right corner of #align1 */
alignment.setHTML(declareA + 'overlay.set("align", {<br> node: "#align1",<br> points: [a.TL, a.TR]<br>});');
descr.setHTML('Align top-left corner of overlay, with top-right corner of #align1');
Y.one('#align1').addClass('aligned-to');
overlay.set("align", {node:"#align1", points: [a.TL, a.TR]});
},
function() {
/* Center overlay in #align2 */
alignment.setHTML(dontDeclareA + 'overlay.set("centered", "#align2");');
descr.setHTML('Center overlay in #align2 <br>(an image)');
Y.one('#align2').addClass('aligned-to');
overlay.set("centered", "#align2");
},
function() {
/* Align top-center edge of overlay with bottom-center edge of #align3 */
alignment.setHTML(declareA + 'overlay.set("align", {<br> node: "#align3",<br> points: [a.TC, a.BC]<br>});');
descr.setHTML('Align top-center edge of overlay with bottom-center edge of #align3');
Y.one('#align3').addClass('aligned-to');
overlay.set("align", {node:"#align3", points:[a.TC, a.BC]});
},
function() {
/* Align right-center edge of overlay with right-center edge of viewport */
alignment.setHTML(declareA + 'overlay.set("align", {<br> points: [a.RC, a.RC]<br>});');
descr.setHTML('Align right-center edge of overlay with right-center edge of viewport');
overlay.set("align", {points:[a.RC, a.RC]});
},
function() {
/* Center overlay in viewport */
alignment.setHTML(dontDeclareA + 'overlay.set("centered", true);');
descr.setHTML('Center overlay in viewport');
overlay.set("centered", true);
},
function() {
/* Center in #overlay-align */
alignment.setHTML(declareA + 'overlay.set("align", {<br> node: "#overlay-align",<br> points: [a.CC, a.CC]<br>});');
descr.setHTML('Center in #overlay-align');
Y.one('#overlay-align').addClass('aligned-to');
overlay.set("align", {node:"#overlay-align", points:[a.CC, a.CC]});
}
];
var step = 0;
var totalSteps = steps.length;
function alignNext() {
stepNumber.setHTML((step+1) + " of " + totalSteps);
Y.all('.aligned-to').removeClass('aligned-to');
steps[step]();
step = (++step)%(totalSteps);
}
alignNext();
Y.on("click", alignNext, "#align");
});
</script></pre>
</div>
</div>
</div>
<div class="yui3-u-1-4">
<div class="sidebar">
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="Shows how to instantiate a basic Overlay instance, and use the Overlay's basic XY positioning support.">
<a href="overlay-xy.html">Basic XY Positioning</a>
</li>
<li data-description="Shows how to create a simple tooltip incorporating the overlay shim feature.">
<a href="overlay-tooltip.html">Simple Tooltip</a>
</li>
<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.">
<a href="overlay-align.html">Alignment Support</a>
</li>
<li data-description="Shows how to use the Overlay's zindex and shim support when positioning Overlays above other elements on the page.">
<a href="overlay-stack.html">Stack Support</a>
</li>
<li data-description="Shows how to modify content in the Overlay's header, body and footer sections.">
<a href="overlay-stdmod.html">Standard Module Support</a>
</li>
<li data-description="Shows how to use Overlay's constrainment support, to limit the XY value which can be set for an Overlay.">
<a href="overlay-constrain.html">Constrain Support</a>
</li>
<li data-description="Shows how to create a simple plugin to retrieve content for the Overlay using the io utility.">
<a href="overlay-io-plugin.html">IO Plugin</a>
</li>
<li data-description="Shows how to create a simple plugin to animate the Overlay's movement and visibility.">
<a href="overlay-anim-plugin.html">Animation Plugin</a>
</li>
</ul>
</div>
</div>
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples That Use This Component</h2>
</div>
<div class="bd">
<ul class="examples">
<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.">
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a>
</li>
<li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
<a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script>
YUI.Env.Tests = {
examples: [],
project: '../assets',
assets: '../assets/overlay',
name: 'overlay-align',
title: 'Alignment Support',
newWindow: '',
auto: false
};
YUI.Env.Tests.examples.push('overlay-xy');
YUI.Env.Tests.examples.push('overlay-tooltip');
YUI.Env.Tests.examples.push('overlay-align');
YUI.Env.Tests.examples.push('overlay-stack');
YUI.Env.Tests.examples.push('overlay-stdmod');
YUI.Env.Tests.examples.push('overlay-constrain');
YUI.Env.Tests.examples.push('overlay-io-plugin');
YUI.Env.Tests.examples.push('overlay-anim-plugin');
YUI.Env.Tests.examples.push('node-focusmanager-button');
YUI.Env.Tests.examples.push('stylesheet-theme');
</script>
<script src="../assets/yui/test-runner.js"></script>
</body>
</html>