PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/js/tooltip.js

https://code.google.com/p/mwenhanced/
JavaScript | 169 lines | 97 code | 18 blank | 54 comment | 35 complexity | 6d953fe2026b09f2989fe523bef3dc49 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, AGPL-1.0, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. +-------------------------------------------------------------------+
  3. | J S - T O O L T I P (v1.2) |
  4. | |
  5. | Copyright Gerd Tentler www.gerd-tentler.de/tools |
  6. | Created: Feb. 15, 2005 Last modified: Apr. 15, 2005 |
  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. |
  10. | |
  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. +-------------------------------------------------------------------+
  16. ======================================================================================================
  17. This script was tested with the following systems and browsers:
  18. - Windows XP: IE 6, NN 4, NN 7, Opera 7, Firefox 1
  19. - Mac OS X: IE 5, Safari 1
  20. If you use another browser or system, this script may not work for you - sorry.
  21. ------------------------------------------------------------------------------------------------------
  22. USAGE:
  23. Use the toolTip-function with mouse-over and mouse-out events (see example below).
  24. - To show a tooltip, use this syntax: toolTip(text, width in pixels, opacity in percent)
  25. Note: width and opacity are optional
  26. - To hide a tooltip, use this syntax: toolTip()
  27. ------------------------------------------------------------------------------------------------------
  28. EXAMPLE:
  29. <a href="#" onMouseOver="toolTip('Just a test', 150)" onMouseOut="toolTip()">some text here</a>
  30. ======================================================================================================
  31. */
  32. function TOOLTIP() {
  33. //----------------------------------------------------------------------------------------------------
  34. // Configuration
  35. //----------------------------------------------------------------------------------------------------
  36. this.width = 250; // width (pixels)
  37. this.bgColor = '#F5F5F5'; // background color
  38. this.textColor = '#000000'; // text color
  39. this.borderColor = '#666666'; // border color
  40. this.opacity = 90; // opacity (percent) - doesn't work with all browsers
  41. this.cursorDistance = 15; // distance from cursor (pixels)
  42. // don't change
  43. this.text = '';
  44. this.obj = 0;
  45. this.sobj = 0;
  46. this.active = false;
  47. // -------------------------------------------------------------------------------------------------------
  48. // Functions
  49. // -------------------------------------------------------------------------------------------------------
  50. this.create = function() {
  51. if(!this.sobj) this.init();
  52. var t = '<div class=tooltip><div>' + this.text + '</div></div>';
  53. if(document.layers) {
  54. t = '<table border=0 cellspacing=0 cellpadding=1><tr><td bgcolor=' + this.borderColor + '>' + t + '</td></tr></table>';
  55. this.sobj.document.write(t);
  56. this.sobj.document.close();
  57. }
  58. else {
  59. this.sobj.border = '1px solid ' + this.borderColor;
  60. this.setOpacity();
  61. if(document.getElementById) document.getElementById('ToolTip').innerHTML = t;
  62. else document.all.ToolTip.innerHTML = t;
  63. }
  64. this.show();
  65. }
  66. this.init = function() {
  67. if(document.getElementById) {
  68. this.obj = document.getElementById('ToolTip');
  69. this.sobj = this.obj.style;
  70. }
  71. else if(document.all) {
  72. this.obj = document.all.ToolTip;
  73. this.sobj = this.obj.style;
  74. }
  75. else if(document.layers) {
  76. this.obj = document.ToolTip;
  77. this.sobj = this.obj;
  78. }
  79. }
  80. this.show = function() {
  81. var ext = (document.layers ? '' : 'px');
  82. var left = mouseX;
  83. if(left + this.width + this.cursorDistance > winX) left += this.cursorDistance;// if(left + this.width + this.cursorDistance > winX) left -= this.width + this.cursorDistance;
  84. else left += this.cursorDistance;
  85. this.sobj.left = left + ext;
  86. this.sobj.top = mouseY + this.cursorDistance + ext;
  87. if(!this.active) {
  88. this.sobj.visibility = 'visible';
  89. this.active = true;
  90. }
  91. }
  92. this.hide = function() {
  93. if(this.sobj) this.sobj.visibility = 'hidden';
  94. this.active = false;
  95. }
  96. this.setOpacity = function() {
  97. this.sobj.filter = 'alpha(opacity=' + this.opacity + ')';
  98. this.sobj.mozOpacity = '.1';
  99. if(this.obj.filters) this.obj.filters.alpha.opacity = this.opacity;
  100. if(!document.all && this.sobj.setProperty) this.sobj.setProperty('-moz-opacity', this.opacity / 100, '');
  101. }
  102. }
  103. //----------------------------------------------------------------------------------------------------
  104. // Build layer, get mouse coordinates and window width, create tooltip-object
  105. //----------------------------------------------------------------------------------------------------
  106. var tooltip = mouseX = mouseY = winX = 0;
  107. if(document.layers) {
  108. document.write('<layer id="ToolTip"></layer>');
  109. document.captureEvents(Event.MOUSEMOVE);
  110. }
  111. else document.write('<div id="ToolTip" style="position:absolute;z-index:99"></div>');
  112. document.onmousemove = getMouseXY;
  113. function getMouseXY(e) {
  114. if(document.all) {
  115. mouseX = event.clientX + document.body.scrollLeft;
  116. mouseY = event.clientY + document.body.scrollTop;
  117. }
  118. else {
  119. mouseX = e.pageX;
  120. mouseY = e.pageY;
  121. }
  122. if(mouseX < 0) mouseX = 0;
  123. if(mouseY < 0) mouseY = 0;
  124. if(document.body && document.body.offsetWidth) winX = document.body.offsetWidth - 25;
  125. else if(window.innerWidth) winX = window.innerWidth - 25;
  126. else winX = screen.width - 25;
  127. if(tooltip && tooltip.active) tooltip.show();
  128. }
  129. function toolTip(text, width, opacity) {
  130. if(text) {
  131. tooltip = new TOOLTIP();
  132. tooltip.text = text;
  133. if(width) tooltip.width = width;
  134. if(opacity) tooltip.opacity = opacity;
  135. tooltip.create();
  136. }
  137. else if(tooltip) tooltip.hide();
  138. }