5
<title>Example: Simple Tooltip</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Example: Simple Tooltip</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><style type="text/css" scoped>
23
/* Hide overlay markup while loading, if js is enabled */
24
.yui3-js-enabled .yui3-overlay-loading {
31
/* Example Layout CSS */
37
<p>This example demonstrates a lightweight and flexible setup for display
39
<p>This displays the tooltip adjacent to the cursor
40
and takes advantage of the shim capabilities of <code>Overlay</code>,
41
allowing the tooltip to properly display over <code>select</code> elements in IE.</p>
51
margin: 10px auto; /*center in viewport*/
55
-moz-padding-start: 0;
56
-webkit-padding-start: 0;
61
.example #tooltip .yui3-widget-bd{
65
background-color: #FFF6D5;
66
border: solid 1px #aa8;
67
padding: 0.2em 0.5em 0.3em;
68
-moz-border-radius: 2px;
69
-webkit-border-radius: 2px;
70
-moz-box-shadow: 3px 3px 5px rgba(0,0,0,0.2);
71
-webkit-box-shadow: 3px 3px 5px rgba(0,0,0,0.2);
77
border: 1px solid #C9C9C9;
82
vertical-align: bottom;
85
.example .list .select{
86
padding: 1.5em 0 0.5em;
88
background-color: #F7DBB2;
94
<div id="tooltip"></div>
96
<div class="yui3-g lists">
97
<div class="yui3-u-1-2"> <!-- see CSS Grids -->
99
<li class="wrench"><img src="../assets/overlay/img/wrench.png"></li>
100
<li class="calipers"><img src="../assets/overlay/img/calipers.png"/></li>
101
<li class="drill"><img src="../assets/overlay/img/drill.png"/></li>
102
<li class="ohm"><img src="../assets/overlay/img/ohm.png"/></li>
103
<li class="level"><img src="../assets/overlay/img/level.png"/></li>
108
<li class="endwrench"><img src="../assets/overlay/img/endwrench.png"/></li>
109
<li class="knife"><img src="../assets/overlay/img/knife.png"/></li>
110
<li class="scissors"><img src="../assets/overlay/img/scissors.png"/></li>
111
<li class="screwdriver"><img src="../assets/overlay/img/screwdriver.png"/></li>
112
<li class="tape"><img src="../assets/overlay/img/tape.png"/></li>
113
<li class="select">Tooltips cover <select><option>select</option></select> elements in IE.</li>
119
YUI().use('overlay', 'event', 'widget-anim', function (Y) {
121
var waitingToShow = false,
123
// array for text to be displayed in tooltips
125
{'name': 'wrench', 'text': "Avoid dropping on toe."},
126
{'name': 'calipers', 'text': 'Dial calipers: +- .001,<br>human hair .004'},
127
{'name': 'drill', 'text': 'Variable-speed and cordless too.'},
128
{'name': 'ohm', 'text': 'Never test microwaves with a volt-ohm meter.'},
129
{'name': 'level', 'text': 'Unreliable in zero gravity conditions.'},
130
{'name': 'endwrench', 'text': '11/16 box-end wrench A.K.A spanner - (British)'},
131
{'name': 'knife', 'text': 'Wants to roll off table and stick in foot.'},
132
{'name': 'scissors', 'text': "Don't run with these."},
133
{'name': 'screwdriver', 'text': 'Not intended for garden weeding.'},
134
{'name': 'tape', 'text': 'Remove before cutting.'},
135
{'name': 'select', 'text': 'Covering select'}
138
var tooltip = new Y.Overlay({
141
}).plug(Y.Plugin.WidgetAnim);
142
tooltip.anim.get('animHide').set('duration', 0.01);
143
tooltip.anim.get('animShow').set('duration', 0.3);
146
// handler that positions and shows the tooltip
147
var onMousemove = function (e) {
149
if (tooltip.get('visible') === false) {
150
// while it's still hidden, move the tooltip adjacent to the cursor
151
Y.one('#tooltip').setStyle('opacity', '0');
152
tooltip.move([(e.pageX + 10), (e.pageY + 20)]);
154
if (waitingToShow === false) {
155
// wait half a second, then show tooltip
156
setTimeout(function(){
157
Y.one('#tooltip').setStyle('opacity', '1');
161
// while waiting to show tooltip, don't let other
162
// mousemoves try to show tooltip too.
163
waitingToShow = true;
165
// loop through the tipText array looking for a match between
166
// the class name and the array literal <code>name</code>
167
for (i = 0; i < tipText.length; i += 1) {
168
if (e.currentTarget.hasClass(tipText[i].name)) {
170
// found a match, so set the content in the tooltip's body
171
tooltip.setStdModContent('body', tipText[i].text);
178
// handler that hides the tooltip
179
var onMouseleave = function (e) {
181
// this check prevents hiding the tooltip
182
// when the cursor moves over the tooltip itself
183
if ((e.relatedTarget) && (e.relatedTarget.hasClass('yui3-widget-bd') === false)) {
185
waitingToShow = false;
189
Y.delegate('mousemove', onMousemove, '.lists', 'li');
190
Y.delegate('mouseleave', onMouseleave, '.lists', 'li');
191
Y.one('#tooltip').on('mouseleave', onMouseleave);
198
<h2>Simple Tooltips</h2>
202
<h3>The Tooltip Markup</h3>
204
<p>We just need a <code>div</code> for the tooltip, and some elements that want to
207
<pre class="code prettyprint"><div id="tooltip"></div>
209
<ul class="list">
210
<li class="wrench"><img src="../assets/overlay/img/wrench.png"/></li>
211
<li class="calipers"><img src="../assets/overlay/img/calipers.png"/></li>
212
<li class="drill"><img src="../assets/overlay/img/drill.png"/></li>
213
<li class="ohm"><img src="../assets/overlay/img/ohm.png"/></li>
214
<li class="level"><img src="../assets/overlay/img/level.png"/></li>
215
</ul></pre>
218
<h3>Setting Up the YUI Instance</h3>
220
<p>We'll use the <code>Overlay</code> module to provide shimming for correctly covering
221
<code><select></code> elements in IE. We'll use <code>Event</code> for its <code>onmouseleave</code> event, and
222
<code>widget-anim</code> as a plugin to provide the fade-in for the tooltip.
225
<pre class="code prettyprint">YUI().use('overlay', 'event', 'widget-anim', function (Y) {
226
// code will go here.
229
<h3>Declare some variables</h3>
231
<p>These variables include an array of strings for various tooltips.
232
If a DOM element has a class matching one of the <code>name</code> values in the <code>tipText</code> array,
233
it will display the corresponding text value in its tooltip when the cursor moves
237
<pre class="code prettyprint">var waitingToShow = false,
239
// array for text to be displayed in tooltips
241
{'name': 'wrench', 'text': "Avoid dropping on toe."},
242
{'name': 'calipers', 'text': 'Dial calipers: +- .001,<br>human hair .004'},
243
{'name': 'drill', 'text': 'Variable-speed and cordless too.'},
244
{'name': 'ohm', 'text': 'Never test microwaves with a volt-ohm meter.'},
245
{'name': 'level', 'text': 'Unreliable in zero gravity conditions.'}
246
// the array continues...
250
<h3>Instantiating The Tooltip</h3>
252
<p>To create an overlay instance for the tooltip,
253
we use the overlay constructor <code>Y.Overlay</code>, specifying the source node
254
as the selector of the tooltip element.
257
<pre class="code prettyprint">var tooltip = new Y.Overlay({
258
srcNode: "#tooltip",
260
}).plug(Y.Plugin.WidgetAnim);
261
tooltip.anim.get('animHide').set('duration', 0.01);
262
tooltip.anim.get('animShow').set('duration', 0.3);
263
tooltip.render();</pre>
266
<h3>Event Handlers</h3>
269
Create event handlers for the mouse events
270
and place them above the listeners code.
272
<pre class="code prettyprint">// handler that positions and shows the tooltip
273
var onMousemove = function (e) {
275
if (tooltip.get('visible') === false) {
276
// while it's still hidden, move the tooltip adjacent to the cursor
277
Y.one('#tooltip').setStyle('opacity', '0');
278
tooltip.move([(e.pageX + 10), (e.pageY + 20)]);
280
if (waitingToShow === false) {
281
// wait half a second, then show tooltip
282
setTimeout(function(){
283
Y.one('#tooltip').setStyle('opacity', '1');
287
// while waiting to show tooltip, don't let other
288
// mousemoves try to show tooltip too.
289
waitingToShow = true;
291
// loop through the tipText array looking for a match between
292
// the class name and the array literal `name`
293
for (i = 0; i < tipText.length; i += 1) {
294
if (e.currentTarget.hasClass(tipText[i].name)) {
296
// found a match, so set the content in the tooltip's body
297
tooltip.setStdModContent('body', tipText[i].text);
304
// handler that hides the tooltip
305
var onMouseleave = function (e) {
307
// this check prevents hiding the tooltip
308
// when the cursor moves over the tooltip itself
309
if ((e.relatedTarget) && (e.relatedTarget.hasClass('yui3-widget-bd') === false)) {
311
waitingToShow = false;
317
<h3>Event Listeners</h3>
320
Add listeners for <code>mousemove</code> and <code>mouseout</code> events
321
to whatever elements you want to have tooltips.
322
Using <code>mousemove</code> instead of <code>mouseover</code>, and using a <code>delay</code> in the <code>transition</code>
323
makes the tooltip fade in adjacent to the cursor.
324
See <a href="../event/index.html#delegation">event delegation</a> for efficient
325
event subscribing to multiple elements.
328
<pre class="code prettyprint">Y.delegate('mousemove', onMousemove, '.lists', 'li');
329
Y.delegate('mouseleave', onMouseleave, '.lists', 'li');
330
Y.one('#tooltip').on('mouseleave', onMouseleave);</pre>
333
This example demonstrates how you can use the existing Overlay component
334
to create a one-off Tooltip for your page.
335
If you plan to re-use the Tooltip on multiple pages,
336
or in a variety of use cases, it's a good idea to encapsulate
337
all the code which makes a tooltip work into a reusable Widget class.
339
<a href="../widget/widget-tooltip.html">Creating a Simple Tooltip Widget with Extensions</a>,
340
shows how you can create a reusable, flexible Tooltip class.
343
<h3>Complete Example Source</h3>
344
<pre class="code prettyprint"><style>
350
margin: 10px auto; /*center in viewport*/
354
-moz-padding-start: 0;
355
-webkit-padding-start: 0;
360
.example #tooltip .yui3-widget-bd{
364
background-color: #FFF6D5;
365
border: solid 1px #aa8;
366
padding: 0.2em 0.5em 0.3em;
367
-moz-border-radius: 2px;
368
-webkit-border-radius: 2px;
369
-moz-box-shadow: 3px 3px 5px rgba(0,0,0,0.2);
370
-webkit-box-shadow: 3px 3px 5px rgba(0,0,0,0.2);
376
border: 1px solid #C9C9C9;
381
vertical-align: bottom;
384
.example .list .select{
385
padding: 1.5em 0 0.5em;
387
background-color: #F7DBB2;
393
<div id="tooltip"></div>
395
<div class="yui3-g lists">
396
<div class="yui3-u-1-2"> <!-- see CSS Grids -->
397
<ul class="list">
398
<li class="wrench"><img src="../assets/overlay/img/wrench.png"></li>
399
<li class="calipers"><img src="../assets/overlay/img/calipers.png"/></li>
400
<li class="drill"><img src="../assets/overlay/img/drill.png"/></li>
401
<li class="ohm"><img src="../assets/overlay/img/ohm.png"/></li>
402
<li class="level"><img src="../assets/overlay/img/level.png"/></li>
405
<div class="yui3-u">
406
<ul class="list">
407
<li class="endwrench"><img src="../assets/overlay/img/endwrench.png"/></li>
408
<li class="knife"><img src="../assets/overlay/img/knife.png"/></li>
409
<li class="scissors"><img src="../assets/overlay/img/scissors.png"/></li>
410
<li class="screwdriver"><img src="../assets/overlay/img/screwdriver.png"/></li>
411
<li class="tape"><img src="../assets/overlay/img/tape.png"/></li>
412
<li class="select">Tooltips cover <select><option>select</option></select> elements in IE.</li>
418
YUI().use('overlay', 'event', 'widget-anim', function (Y) {
420
var waitingToShow = false,
422
// array for text to be displayed in tooltips
424
{'name': 'wrench', 'text': "Avoid dropping on toe."},
425
{'name': 'calipers', 'text': 'Dial calipers: +- .001,<br>human hair .004'},
426
{'name': 'drill', 'text': 'Variable-speed and cordless too.'},
427
{'name': 'ohm', 'text': 'Never test microwaves with a volt-ohm meter.'},
428
{'name': 'level', 'text': 'Unreliable in zero gravity conditions.'},
429
{'name': 'endwrench', 'text': '11/16 box-end wrench A.K.A spanner - (British)'},
430
{'name': 'knife', 'text': 'Wants to roll off table and stick in foot.'},
431
{'name': 'scissors', 'text': "Don't run with these."},
432
{'name': 'screwdriver', 'text': 'Not intended for garden weeding.'},
433
{'name': 'tape', 'text': 'Remove before cutting.'},
434
{'name': 'select', 'text': 'Covering select'}
437
var tooltip = new Y.Overlay({
438
srcNode: "#tooltip",
440
}).plug(Y.Plugin.WidgetAnim);
441
tooltip.anim.get('animHide').set('duration', 0.01);
442
tooltip.anim.get('animShow').set('duration', 0.3);
445
// handler that positions and shows the tooltip
446
var onMousemove = function (e) {
448
if (tooltip.get('visible') === false) {
449
// while it's still hidden, move the tooltip adjacent to the cursor
450
Y.one('#tooltip').setStyle('opacity', '0');
451
tooltip.move([(e.pageX + 10), (e.pageY + 20)]);
453
if (waitingToShow === false) {
454
// wait half a second, then show tooltip
455
setTimeout(function(){
456
Y.one('#tooltip').setStyle('opacity', '1');
460
// while waiting to show tooltip, don't let other
461
// mousemoves try to show tooltip too.
462
waitingToShow = true;
464
// loop through the tipText array looking for a match between
465
// the class name and the array literal `name`
466
for (i = 0; i < tipText.length; i += 1) {
467
if (e.currentTarget.hasClass(tipText[i].name)) {
469
// found a match, so set the content in the tooltip's body
470
tooltip.setStdModContent('body', tipText[i].text);
477
// handler that hides the tooltip
478
var onMouseleave = function (e) {
480
// this check prevents hiding the tooltip
481
// when the cursor moves over the tooltip itself
482
if ((e.relatedTarget) && (e.relatedTarget.hasClass('yui3-widget-bd') === false)) {
484
waitingToShow = false;
488
Y.delegate('mousemove', onMousemove, '.lists', 'li');
489
Y.delegate('mouseleave', onMouseleave, '.lists', 'li');
490
Y.one('#tooltip').on('mouseleave', onMouseleave);
494
</body></pre>
500
<div class="yui3-u-1-4">
501
<div class="sidebar">
505
<div class="sidebox">
507
<h2 class="no-toc">Examples</h2>
511
<ul class="examples">
514
<li data-description="Shows how to instantiate a basic Overlay instance, and use the Overlay's basic XY positioning support.">
515
<a href="overlay-xy.html">Basic XY Positioning</a>
520
<li data-description="Shows how to create a simple tooltip incorporating the overlay shim feature.">
521
<a href="overlay-tooltip.html">Simple Tooltip</a>
526
<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.">
527
<a href="overlay-align.html">Alignment Support</a>
532
<li data-description="Shows how to use the Overlay's zindex and shim support when positioning Overlays above other elements on the page.">
533
<a href="overlay-stack.html">Stack Support</a>
538
<li data-description="Shows how to modify content in the Overlay's header, body and footer sections.">
539
<a href="overlay-stdmod.html">Standard Module Support</a>
544
<li data-description="Shows how to use Overlay's constrainment support, to limit the XY value which can be set for an Overlay.">
545
<a href="overlay-constrain.html">Constrain Support</a>
550
<li data-description="Shows how to create a simple plugin to retrieve content for the Overlay using the io utility.">
551
<a href="overlay-io-plugin.html">IO Plugin</a>
556
<li data-description="Shows how to create a simple plugin to animate the Overlay's movement and visibility.">
557
<a href="overlay-anim-plugin.html">Animation Plugin</a>
571
<div class="sidebox">
573
<h2 class="no-toc">Examples That Use This Component</h2>
577
<ul class="examples">
596
<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.">
597
<a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
602
<li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
603
<a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
616
<script src="../assets/vendor/prettify/prettify-min.js"></script>
617
<script>prettyPrint();</script>