2 +-------------------------------------------------------------------+
3 | J S - T O O L T I P (v2.1) |
5 | Copyright Gerd Tentler www.gerd-tentler.de/tools |
6 | Created: Feb. 15, 2005 Last modified: Apr. 9, 2007 |
7 +-------------------------------------------------------------------+
8 | This program may be used and hosted free of charge by anyone for |
9 | personal purpose as long as this copyright notice remains intact. |
11 | Obtain permission before selling the code for this program or |
12 | hosting this software on a commercial website or redistributing |
13 | this software over the Internet or in any other medium. In all |
14 | cases copyright must remain intact. |
15 +-------------------------------------------------------------------+
17 ======================================================================================================
19 This script was tested with the following systems and browsers:
21 - Windows XP: IE 6, NN 7, Opera 7 + 9, Firefox 2
22 - Mac OS X: IE 5, Safari 1
24 If you use another browser or system, this script may not work for you - sorry.
26 ------------------------------------------------------------------------------------------------------
30 Use the toolTip-function with mouse-over and mouse-out events (see example below).
32 - To show a tooltip, use this syntax: toolTip(text, width in pixels, opacity in percent)
33 Note: width and opacity are optional. Opacity is not supported by all browsers.
35 - To hide a tooltip, use this syntax: toolTip()
37 ------------------------------------------------------------------------------------------------------
41 <a href="#" onMouseOver="toolTip('Just a test', 150)" onMouseOut="toolTip()">some text here</a>
43 ======================================================================================================
46 var OP = (navigator.userAgent.indexOf('Opera') != -1);
47 var IE = (navigator.userAgent.indexOf('MSIE') != -1 && !OP);
48 var GK = (navigator.userAgent.indexOf('Gecko') != -1);
49 var SA = (navigator.userAgent.indexOf('Safari') != -1);
50 var DOM = document.getElementById;
55 //----------------------------------------------------------------------------------------------------
57 //----------------------------------------------------------------------------------------------------
58 this.width = 400; // width (pixels)
59 this.bgColor = "#C0C0C0"; // background color
60 this.textFont = "Arial"; // text font family
61 this.textSize = 14; // text font size (pixels)
62 this.textColor = "#000000"; // text color
63 this.border = "1px dashed #000000"; // border (CSS spec: size style color, e.g. "1px solid #D00000")
64 this.opacity = 90; // opacity (0 - 100); not supported by all browsers
65 this.cursorDistance = 5; // distance from mouse cursor (pixels)
72 this.allowMove = true;
73 //----------------------------------------------------------------------------------------------------
75 //----------------------------------------------------------------------------------------------------
76 this.create = function() {
77 if(!this.obj) this.init();
79 var s = (this.textFont ? 'font-family:' + this.textFont + '; ' : '') +
80 (this.textSize ? 'font-size:' + this.textSize + 'px; ' : '') +
81 (this.border ? 'border:' + this.border + '; ' : '') +
82 (this.textColor ? 'color:' + this.textColor + '; ' : '');
84 var t = '<table border="0" cellspacing="0" cellpadding="4" style="margin:0" width="' + this.width + 'px"><tr>' +
85 '<td align="center"' + (s ? ' style="' + s + '"' : '') + '>' + this.text +
88 if(DOM || IE) this.obj.innerHTML = t;
89 if(DOM) this.height = this.obj.offsetHeight;
90 else if(IE) this.height = this.obj.style.pixelHeight;
91 if(this.bgColor) this.obj.style.backgroundColor = this.bgColor;
98 this.init = function() {
99 if(DOM) this.obj = document.getElementById('ToolTip');
100 else if(IE) this.obj = document.all.ToolTip;
103 this.move = function() {
104 var winX = getWinX() - (((GK && !SA) || OP) ? 17 : 0);
105 var winY = getWinY() - (((GK && !SA) || OP) ? 17 : 0);
109 if(x + this.width + this.cursorDistance > winX + getScrX())
110 x -= this.width + this.cursorDistance;
111 else x += this.cursorDistance;
113 if(y + this.height + this.cursorDistance > winY + getScrY())
115 else y += this.cursorDistance;
117 this.obj.style.left = x + 'px';
118 this.obj.style.top = y + 'px';
121 this.show = function() {
122 this.obj.style.zIndex = 69;
124 this.obj.style.visibility = 'visible';
127 this.hide = function() {
128 this.obj.style.zIndex = -1;
130 this.obj.style.visibility = 'hidden';
133 this.setOpacity = function() {
134 this.obj.style.opacity = this.opacity / 100;
135 this.obj.style.MozOpacity = this.opacity / 100;
136 this.obj.style.KhtmlOpacity = this.opacity / 100;
137 this.obj.style.filter = 'alpha(opacity=' + this.opacity + ')';
141 //----------------------------------------------------------------------------------------------------
143 //----------------------------------------------------------------------------------------------------
146 if(window.pageXOffset)
147 offset = window.pageXOffset;
148 else if(document.documentElement && document.documentElement.scrollLeft)
149 offset = document.documentElement.scrollLeft;
150 else if(document.body && document.body.scrollLeft)
151 offset = document.body.scrollLeft;
157 if(window.pageYOffset)
158 offset = window.pageYOffset;
159 else if(document.documentElement && document.documentElement.scrollTop)
160 offset = document.documentElement.scrollTop;
161 else if(document.body && document.body.scrollTop)
162 offset = document.body.scrollTop;
168 if(window.innerWidth)
169 size = window.innerWidth;
170 else if(document.documentElement && document.documentElement.clientWidth)
171 size = document.documentElement.clientWidth;
172 else if(document.body && document.body.clientWidth)
173 size = document.body.clientWidth;
174 else size = screen.width;
180 if(window.innerHeight)
181 size = window.innerHeight;
182 else if(document.documentElement && document.documentElement.clientHeight)
183 size = document.documentElement.clientHeight;
184 else if(document.body && document.body.clientHeight)
185 size = document.body.clientHeight;
186 else size = screen.height;
190 function getMouseXY(e) {
191 if(e && e.pageX != null) {
195 else if(event && event.clientX != null) {
196 mouseX = event.clientX + getScrX();
197 mouseY = event.clientY + getScrY();
199 if(mouseX < 0) mouseX = 0;
200 if(mouseY < 0) mouseY = 0;
201 if(tooltip && tooltip.active && tooltip.allowMove) tooltip.move();
204 function toolTip(text, width, opacity) {
206 tooltip = new TOOLTIP();
208 if(width) tooltip.width = width;
209 if(opacity) tooltip.opacity = opacity;
212 else if(tooltip) tooltip.hide();
215 function menu(text, width, opacity) {
217 tooltip = new TOOLTIP();
219 if(width) tooltip.width = width;
220 if(opacity) tooltip.opacity = opacity;
222 tooltip.allowMove = false;
224 else if(tooltip) tooltip.hide();
227 //----------------------------------------------------------------------------------------------------
229 //----------------------------------------------------------------------------------------------------
230 document.write('<div id="ToolTip" style="position:absolute; visibility:hidden"></div>');
232 //----------------------------------------------------------------------------------------------------
234 //----------------------------------------------------------------------------------------------------
237 document.onmousemove = getMouseXY;
239 //----------------------------------------------------------------------------------------------------