Widget.Tooltip - Unobtrusive javascript class for create and handle tooltips
<!-- XHTML page -->
<p id="text1">some text...</p>
// javascript code
new Widget.Tooltip({ elementId: 'text1', tooltipContent: 'tooltip for some text' });
This nice class implements some methods for adding tooltips on document elements. The tooltip can contain plain text or some XHTML markup. When the user moves the mouse over the reference element, the tooltip is shown. By setting some configurable options is possible to control the tooltip behaviour, by default the tooltip follows the mouse when is moved, stops and freezes on a left button click, another click will unlock the tooltip.
Tested under Firefox 2, Internet Explorer 6/7, Opera 9.
Class constructor, returns a new Widget.Tooltip object.
Object literal where must be specified the element id on which add the tooltip, the tooltip content and other optional properties that overwrites the defaults.
Properties are:
elementIdID of the element on which add a tooltip. mandatory!
enableLockDefines if the tooltip must freeze on the current mouse position by clicking the reference element (default is 1=enabled, set to 0 to disable).
enableMoveDefines if the tooltip must follow the mouse pointer when is moved (default is 1=enabled, set to 0 to disable).
fadeOutTime expressed in milliseconds after which the tooltip will be hidden (default 0=disabled).
offsetXHorizontal distance in pixels from the mouse pointer of the tooltip top-left corner (default is 16).
offsetYVertical distance in pixels from the mouse pointer of the tooltip top-left corner (default is 16).
tooltipClassCSS class to assign to the tooltip div element (default is `tooltip'), useful for adding some make-up to the tooltip.
tooltipContentContent of the tooltip. Usually some text you want to show. You can pass also an XHTML string. If nothing is provided, the default text `Empty tooltip!' is used.
Hides the tooltip.
Moves the tooltip following the mouse pointer (depending on the enableMove flag setting).
Shows the tooltip.
Freezes/unfreezes the tooltip by switching the internal lock flag (depending on the enableLock flag setting).
<!-- you can initialize the class within your XHTML page like this -->
<p id="text1">some text...</p>
<p>an image<br />
<img src="ghost.png" /></p>
<p>you can also have a <a id="anchor1" href="somewhere_else">link</a> within the text on which you want to add a tooltip.</p>
<script type="text/javascript">
new Widget.Tooltip({ elementId: 'text1', tooltipContent: 'tooltip for some text (fades out in 5 seconds)', fadeout: 5000 });
new Widget.Tooltip({ elementId: 'img1', tooltipContent: 'where is the image?!<br />click <a href="#" onclick="showImage(\'img1\')">here</a>...' });
new Widget.Tooltip({ elementId: 'anchor1', tooltipContent: 'a link must be clickable, so you should set enableLock to 0 on this tooltip!', enableLock: 0 });
</script>
// or in a more unobtrusive way put the new in an included javascript file.
// be sure to call the new when the page is loaded!
// a cross-browser way may be this
if( document.addEventListener )
{
document.addEventListener(
'load',
function() {
new Widget.Tooltip({ elementId: 'text1', tooltipContent: 'tooltip for some text (fades out in 5 seconds)', fadeout: 5000 });
new Widget.Tooltip({ elementId: 'img1', tooltipContent: 'where is the image?!<br />click <a href="#" onclick="showImage(\'img1\')">here</a>...' });
new Widget.Tooltip({ elementId: 'anchor1', tooltipContent: 'a link must be clickable, so you should set enableLock to 0 on this tooltip!', enableLock: 0 });
},
false
);
return true;
}
else if( document.attachEvent )
{
return document.attachEvent(
'onload',
function() {
new Widget.Tooltip({ elementId: 'text1', tooltipContent: 'tooltip for some text (fades out in 5 seconds)', fadeout: 5000 });
new Widget.Tooltip({ elementId: 'img1', tooltipContent: 'where is the image?!<br />click <a href="#" onclick="showImage(\'img1\')">here</a>...' });
new Widget.Tooltip({ elementId: 'anchor1', tooltipContent: 'a link must be clickable, so you should set enableLock to 0 on this tooltip!', enableLock: 0 });
}
);
}
Official web page at http://www.sabadelli.it/edoardo/projects/javascript/widget.tooltip
JSAN http://openjsan.org/
Edoardo Sabadelli - http://www.sabadelli.it/edoardo
Copyright (c) 2007 Edoardo Sabadelli. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the terms of the Artistic license.