DOM Tooltip Library HOWTO
Usage
This library offers a variety of ways to enable custom tooltips. The standard way of creating a tooltip is to add the domTT_activate() call to one of the event handlers of the target element. Tips can also be created programatically, which can be used for popping up inline windows. Another option is to call the method domTT_replaceTitles(), which replaces all elements containing the class 'tooltip' and the title attribute with a custom tooltip on mouseover.
Examples
<a href="index.html" onmouseover="domTT_activate(this, event, 'content', 'My first tooltip', 'trail', true);">sample link</a>
Options
The only required option to create a tooltip is the 'content' option. Most of the options below have a default value which is held in a global variable. If the option is specified, it overrides the default. The options are summarized below.
- caption [text|xhtml|DOM Node]
- An auto-generated caption will be created if this text is present. Set this to
false
when creating a sticky tip to prevent the automatic caption with close link.
- content [text|xhtml|DOM Node|function] (required)
- The main content of the tip, which may be XHTML text. Note that when using a function, it is only called when the tip is activated, so it is necessary to use 'closeMethod' of 'destroy' to redraw each time.
- clearMouse [boolean]
- This option flags whether the tip should attempt to avoid the mouse when the direction is south.
- closeAction ['hide'|'destroy']
- Determines if the tip should be destroyed (removed from DOM) or just hidden when deactivated. (If fading is used, hiding is forced)
- closeLink [text|xhtml|DOM Node]
- The text that should be used for the auto-generated close link used for sticky tips.
- delay [ms]
- Time in milliseconds before the tip appears.
- direction ['southeast'|'southwest'|'northeast'|'northwest'|'north'|'south']
- The position of the tip relative to the mouse.
- draggable [boolean]
- Determines of the sticky tooltip can be dragged.
- fade ['in'|'out'|'neither'|'both']
- Sets the alpha effect when the tip is created or destroyed.
- fadeMax [0-100]
- Sets the maximum alpha that should be used for the alpha effect. (Minimum is always 0, or invisible)
- grid [px]
- Snaps the trailing to a grid, so that the tip only moves after a set number of pixels.
- id [string]
- The XML id that should be assigned to the tip. Using this setting allows for external manipulation of the tip.
- inframe [boolean]
- Hints that the tooltip is inside an iframe, so that the tip can be manipulated from the parent frame.
- lifetime [ms]
- The duration of time before the tooltip is automatically terminated, as long as it is still activated.
- offsetX [px]
- The left-to-right offset from the event where the tip should be placed.
- offsetY [px]
- The top-to-bottom offset from the event where the tip should be placed.
- parent [DOM Node]
- The parent node on which the tip should be appended. Usually used for tips with a relative position.
- position ['absolute'|'relative'|'fixed']
- The style position of the tip. (In most cases, the position is 'absolute')
- predefined [string]
- A reference to a previously defined tooltip using domTT_addPredefined()
- statusText [string]
- Sets the text to be used in the statusbar when the tip is activated. If used with mouseover, it is necessary to wrap the domTT_activate() call in 'return domTT_true()'.
- styleClass [string]
- The class that will be assigned to the tip. The contents of the tip is assigned the class 'content' and the caption of the tip is assigned the class 'caption'.
- type ['greasy'|'sticky'|'velcro']
- Sets the fate of the tip. A greasy tip disappears when a mouse out occurs on owner. A sticky tip stays active until explicitly destroyed (a caption is also forced). A velcro tip disappears when a mouse out occurs on the tip itself.
- trail [true|false|'x'|'y']
- On which axis the tip should be updated when the mouse moves. A value of true updates on both axes.
- lazy [boolean]
- Whether or not to enable a delay when updating the mouse position of a trailing tip (drunk tooltip).
- width [px]
- A manual width for the tip.
- maxWidth [px]
- A manual maximum width for the tip. (In Firefox, this is controlled by the max-width style of the element)
- x [px]
- The absolute x position to be used for the tip location. This can be a calculated value, such as 'this.offsetLeft + 5'.
- y [px]
- The absolute y position to be used for the tip location. This can be a calculated value, such as 'this.offsetTop + 5'.
Function Reference
The DOM tooltip library offers a handful of functions, prefixed with 'domTT_', to aid in manipulating the tooltips. The following is a reference of these function calls.
- domTT_activate()
- The primary method for activating a tooltip, typically placed in an event handler such as
onmouseover
. The first two arguments must always be this
and event
, respectively. These two variable capture the environment on which to act. The rest of the arguments alternate between option name and option value from the list above.
- domTT_addPredefined()
- For times when tooltip creation should be seperated from activation, this method can be used to prepare the tooltips. The first argument is a unique name for the configuration, and the rest of the arguments alternate between option name and option value from the list of options above. A preconfigured tip can be reused any number of times, and the activate call can override options in the predefined configuration. A typical activate call for a predefined tooltip looks like:
domTT_activate(this, event, 'predefined', 'mytip')
- domTT_close()
- A more flexible alternative to
domTT_deactivate()
, this function assists in immediately closing the tooltip. The argument can either be a child object of the tip, the tip id or the owner id. From within a tooltip, the call is typically: domTT_close(this)
- domTT_deactivate()
- In most cases, this method is automatically registered on the target element when
domTT_activate()
is first called. However, it may be necessary to programmatically close a tooltip. In this case, this method can be used. It requires one parameter, which can be the ID of the tooltip, the ID of the target or the target object itself.
- domTT_mouseout()
- When a tooltip is activate, it automatically registers a mouseout handler to close to tooltip unless one already exists. If you intend to have a custom mouseout handler on the target that activates the tip, you must make a call to
domTT_mouseout(this, event)
from within your mouseout handler. Otherwise, the tooltip will never be deactivated!
- domTT_update()
- If you find that you need to update the contents or caption of a tooltip after it has been created, you will find this function convenient. You must specify a tooltip id, target id or target node and new text or XHTML content and it will be used to replace the content of the existing tooltip. The third parameter allows you to specify either 'content' or 'caption', though it is optional and 'content' is the default.
- makeTrue()/makeFalse()
- These methods are indended soley for coaxing the return value of an event handler. Of times, it is necessary to return either true or false to the event handler, regardless of what the function in the handler might return. These methods will guarantee that result. For instance, the
onclick
handler must return false for a link or else the browser will follow the target href. Conversely, the handler must return true or else the browser will stall. These methods just help to achieve the desired behavior.
Global Settings
All of the options above can be used a global settings by prepending the 'domTT_' prefix. These assignments are a good way to establish common behaviors for your tooltips on the webpage. A couple of additional settings are documented here. Be sure to include your custom values below the point when the domTT library is included so that they override the default values.
- domTT_oneOnly
- Set to this
true
to allow only one active tip at a time. When a tooltip is created, it will deactivate the last opened tooltip. You can access the last opened tooltip using the global variable domTT_lastOpened
- fading
- In order to use the fading feature, you must include the library fadomatic.js in your web page. See examples #5.
- dragging
- In order to drag tooltips, you must include the library domTT_drag.js in your web page. See example #4.
- domTT_useGlobalMousePosition
- This setting will allow domTT to register an event handler on the document to track the position of the mouse. Registering this event allows the trailing feature to behave more smoothly.
- domTT_screenEdgeDetection
- Enable the screen edge detection feature, which will prevent the tip from leaving the page. Setting this to
false
disables this feature.
- domTT_screenEdgePadding
- When using screen edge detection, this setting specifies how close to the edge the tip should be allowed to travel.
- domTT_detectCollisions
- If you would rather not having the select boxes and other interfering elements hidden when a visual collision is detected, you may turn off the functionality globally using this flag.
Common Problems
- If tips are not disappearing in low latency situations, use
onmouseover = domTT_deactivate(this.id);
to be sure it is discarded. (It seems, though, that a bug fix in 0.7.3 solved this problem.)
- If the error 'operation aborted' is being thrown in IE, use the domTT_postponeActivation along with domTT_documentLoaded to prevent these types of page load problems.
- To get dynamic updates on your tips using AJAX, you can either address the tip by its id, manipulate the node linked to the content, or use this domTT_update method. This point to remember is that the content is only processed when the tip is activated.
- Disabled form fields do not have positions in the page. This prevents tooltips from being placed next to them. In order to use tooltips with form fields that are inactive, please use the onfocus="this.blur()" trick along with appropriate visual styling. In general, the disabled attribute causes other side-effects anyway.