( Please read this before considering using it)
Description
Almost all browser support some form of "tooltip" natively, via the "TITLE" attribute of HTML element. However, this feature is inconsistently implemented, and browser limitation make this unpractical or useless depending on what you are looking for. Notably, you can't insert HTML content on those tooltips, and only a few browsers accept multi-line values. This library replaces the default handling of TITLE attributes by a mecanism which solve those issues.
How to use it
Mouse-over tooltips
Those tooltips are activated when the mouse rest of the element area, and they hide automatically when the mouse leaves this area. All tooltips are mouseover by default.
| HTML |
<span title="Simple tooltip message"> this area </span> |
<span title="Tooltip message|Simple tooltip message" this area </span> |
|---|---|---|
| UI | Mouse over [this area] | A [tooltip] with a caption |
Focus tooltips (form fields)
In addition to trigger the tooltip visibility with mouse position, you may also choose to display tooltips when the keyboard focus enter a form field; this is a convenient way of guiding end user input. When this tooltip is opened this way, its position is choosen so that it does not cover the field.
| HTML |
<form mjstooltipmode="focus">
...
</span>
|
|---|---|
| UI |
Complex tooltip messages
If your tooltip message contains HTML tags and is a little more complex than a simple string, you may find unpractical the use of TITLE attribute. The tooltip module offer an indirection mecanism which let you define the tooltip content somewhere as part of your document (and with hidden visibility) so that you can compose the message with your favorite editor, without dealing with awful quoting problems (remember, the TITLE attribute must be quoted, and what you put inside need backslashed-quoting \" or \').
In addition to an easier definition of tooltip content, this feature makes possible to recycle the same message for multiple nodes; this may be very interesting if you deal with large set of data having common tooltips.
| 1. Defines the tooltip message somewhere
(in a hidden SPAN for example)
|
|
| HTML |
<span id="aComplexTooltip" style="visibility:hidden;"> <p>Here is a Sample long tooltip message, using <i>arbitrary</i> HTML content:</p> |
|---|---|
| 2. Then, reference the message by
its ID via the "#ID" syntax:
|
|
| HTML |
<span title="Complex tooltip|#aComplexTooltip">complex tooltip</span>
|
| UI | Mouse over [complex
tooltip] Mouse over [same complex tooltip] |
Note: only the body of the tooltip message can be set this way; the title must be entered as part of the TITLE attribute.
Large tooltip messages
By default, the tooltip module do not apply specific size adjustment on tooltip message. This is usually fine when dealing with small messages, but this may be problematic with large messages. Instead of statically sizing tooltip messages via a CSS style, you can define the maximum width that a tooltip can use by setting the "mjsTooltipMaxWidth" attribute; this size is set in pixels (do not use the "px" suffix though).
This attribute is inherited; hence you can set it once for multiple tooltips (at the BODY tag level for example).
| HTML |
<span mjstooltipmaxwidth=300
title="Large tooltip|#aLargeTooltip">large tooltip
</span>
|
|---|---|
| UI | Mouse over [large
tooltip] Mouse over [large tooltip with 'mjstooltipmaxwidth' attribute] |
Styling tooltips
In order to make this toolkit instantly usable, the tooltip module generates hardcoded styles for the tip message which mimic the usual rendering for bubble information in most platform (a pale yellow background and a thin black border). If you want to adjust tooltip rendering, you can instruct the module to attach an arbitrary CSS class to the tooltip, which let you customize everything as you wish by editing a few styles. This is done by specifying the attribute "mjstooltipclass". This attribute is inherited, hence attaching this to the BODY tag will redefine all tooltips of the document.
In order to properly implement styling, you need to know hos the tooltip content is organized. The tooltip module used a few CSS classes for each of its components:
|
<DIV class="[yourClass]"> <DIV class="[yourClass]_caption"> Tooltip caption </DIV> <DIV class="[yourClass]_body"> Tooltip content </DIV> </DIV> |
Note: in this sample, [yourclass] is the value which you have selected via the "mjstooltipclass" attribute. |
Here is an example:
| CSS |
.MyTooltip |
|---|---|
| HTML |
<span mjstooltipclass="MyTooltip" title="Title|Customized tooltip">
custom tooltip style
</span>
|
| UI | Mouse over [custom tooltip style] |
Technical notes
How it works
At initialization time, when the document is loaded, the module goes over all elements having a TITLE attribute, compute the HTML data which will be used when showing the tooltip, attach it to the element, and remove the TITLE attribute so that the browser does not use its own handling of titles. In the meantime, appropriate event handler are associated with elements having a title.JavaScript API
mjs_setTooltipClass(className)
mjs_setTooltipShadowSize(sz)
mjs_setTooltipMaxWidth(sz)
Here is a Sample long tooltip message, using arbitrary HTML content:
- List item #1
- List item #2