jquery.tiptip.min.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * TipTip
  3. * Copyright 2010 Drew Wilson
  4. * www.drewwilson.com
  5. * code.drewwilson.com/entry/tiptip-jquery-plugin
  6. *
  7. * Version 1.3 - Updated: Mar. 23, 2010
  8. *
  9. * This Plug-In will create a custom tooltip to replace the default
  10. * browser tooltip. It is extremely lightweight and very smart in
  11. * that it detects the edges of the browser window and will make sure
  12. * the tooltip stays within the current window size. As a result the
  13. * tooltip will adjust itself to be displayed above, below, to the left
  14. * or to the right depending on what is necessary to stay within the
  15. * browser window. It is completely customizable as well via CSS.
  16. *
  17. * This TipTip jQuery plug-in is dual licensed under the MIT and GPL licenses:
  18. * http://www.opensource.org/licenses/mit-license.php
  19. * http://www.gnu.org/licenses/gpl.html
  20. */
  21. (function($) {
  22. $.fn.tipTip = function(options) {
  23. var defaults = {
  24. activation: "hover",
  25. keepAlive: false,
  26. maxWidth: "200px",
  27. edgeOffset: 3,
  28. defaultPosition: "bottom",
  29. delay: 400,
  30. fadeIn: 200,
  31. fadeOut: 200,
  32. attribute: "title",
  33. content: false,
  34. enter: function() {},
  35. exit: function() {}
  36. };
  37. var opts = $.extend(defaults, options);
  38. if ($("#tiptip_holder").length <= 0) {
  39. var tiptip_holder = $('<div id="tiptip_holder" style="max-width:' + opts.maxWidth + ';"></div>');
  40. var tiptip_content = $('<div id="tiptip_content"></div>');
  41. var tiptip_arrow = $('<div id="tiptip_arrow"></div>');
  42. $("body").append(tiptip_holder.html(tiptip_content).prepend(tiptip_arrow.html('<div id="tiptip_arrow_inner"></div>')))
  43. } else {
  44. var tiptip_holder = $("#tiptip_holder");
  45. var tiptip_content = $("#tiptip_content");
  46. var tiptip_arrow = $("#tiptip_arrow")
  47. }
  48. return this.each(function() {
  49. var org_elem = $(this);
  50. if (opts.content) {
  51. var org_title = opts.content
  52. } else {
  53. var org_title = org_elem.attr(opts.attribute)
  54. }
  55. if (org_title != "") {
  56. if (!opts.content) {
  57. org_elem.removeAttr(opts.attribute)
  58. }
  59. var timeout = false;
  60. if (opts.activation == "hover") {
  61. org_elem.hover(function() {
  62. active_tiptip()
  63. }, function() {
  64. if (!opts.keepAlive) {
  65. deactive_tiptip()
  66. }
  67. });
  68. if (opts.keepAlive) {
  69. tiptip_holder.hover(function() {}, function() {
  70. deactive_tiptip()
  71. })
  72. }
  73. } else if (opts.activation == "focus") {
  74. org_elem.focus(function() {
  75. active_tiptip()
  76. }).blur(function() {
  77. deactive_tiptip()
  78. })
  79. } else if (opts.activation == "click") {
  80. org_elem.click(function() {
  81. active_tiptip();
  82. return false
  83. }).hover(function() {}, function() {
  84. if (!opts.keepAlive) {
  85. deactive_tiptip()
  86. }
  87. });
  88. if (opts.keepAlive) {
  89. tiptip_holder.hover(function() {}, function() {
  90. deactive_tiptip()
  91. })
  92. }
  93. }
  94. function active_tiptip() {
  95. opts.enter.call(this);
  96. tiptip_content.html(org_title);
  97. tiptip_holder.hide().removeAttr("class").css("margin", "0");
  98. tiptip_arrow.removeAttr("style");
  99. var top = parseInt(org_elem.offset()['top']);
  100. var left = parseInt(org_elem.offset()['left']);
  101. var org_width = parseInt(org_elem.outerWidth());
  102. var org_height = parseInt(org_elem.outerHeight());
  103. var tip_w = tiptip_holder.outerWidth();
  104. var tip_h = tiptip_holder.outerHeight();
  105. var w_compare = Math.round((org_width - tip_w) / 2);
  106. var h_compare = Math.round((org_height - tip_h) / 2);
  107. var marg_left = Math.round(left + w_compare);
  108. var marg_top = Math.round(top + org_height + opts.edgeOffset);
  109. var t_class = "";
  110. var arrow_top = "";
  111. var arrow_left = Math.round(tip_w - 12) / 2;
  112. if (opts.defaultPosition == "bottom") {
  113. t_class = "_bottom"
  114. } else if (opts.defaultPosition == "top") {
  115. t_class = "_top"
  116. } else if (opts.defaultPosition == "left") {
  117. t_class = "_left"
  118. } else if (opts.defaultPosition == "right") {
  119. t_class = "_right"
  120. }
  121. var right_compare = (w_compare + left) < parseInt($(window).scrollLeft());
  122. var left_compare = (tip_w + left) > parseInt($(window).width());
  123. if ((right_compare && w_compare < 0) || (t_class == "_right" && !left_compare) || (t_class == "_left" && left < (tip_w + opts.edgeOffset + 5))) {
  124. t_class = "_right";
  125. arrow_top = Math.round(tip_h - 13) / 2;
  126. arrow_left = -12;
  127. marg_left = Math.round(left + org_width + opts.edgeOffset);
  128. marg_top = Math.round(top + h_compare)
  129. } else if ((left_compare && w_compare < 0) || (t_class == "_left" && !right_compare)) {
  130. t_class = "_left";
  131. arrow_top = Math.round(tip_h - 13) / 2;
  132. arrow_left = Math.round(tip_w);
  133. marg_left = Math.round(left - (tip_w + opts.edgeOffset + 5));
  134. marg_top = Math.round(top + h_compare)
  135. }
  136. var top_compare = (top + org_height + opts.edgeOffset + tip_h + 8) > parseInt($(window).height() + $(window).scrollTop());
  137. var bottom_compare = ((top + org_height) - (opts.edgeOffset + tip_h + 8)) < 0;
  138. if (top_compare || (t_class == "_bottom" && top_compare) || (t_class == "_top" && !bottom_compare)) {
  139. if (t_class == "_top" || t_class == "_bottom") {
  140. t_class = "_top"
  141. } else {
  142. t_class = t_class + "_top"
  143. }
  144. arrow_top = tip_h;
  145. marg_top = Math.round(top - (tip_h + 5 + opts.edgeOffset))
  146. } else if (bottom_compare || (t_class == "_top" && bottom_compare) || (t_class == "_bottom" && !top_compare)) {
  147. if (t_class == "_top" || t_class == "_bottom") {
  148. t_class = "_bottom"
  149. }
  150. }
  151. if (t_class == "_right_top" || t_class == "_left_top") {
  152. marg_top = marg_top + 5
  153. } else if (t_class == "_right_bottom" || t_class == "_left_bottom") {
  154. marg_top = marg_top - 5
  155. }
  156. if (t_class == "_left_top" || t_class == "_left_bottom") {
  157. marg_left = marg_left + 5
  158. }
  159. tiptip_arrow.css({
  160. "margin-left": arrow_left + "px",
  161. "margin-top": arrow_top + "px"
  162. });
  163. tiptip_holder.css({
  164. "margin-left": marg_left + "px",
  165. "margin-top": marg_top + "px"
  166. }).attr("class", "tip" + t_class);
  167. if (timeout) {
  168. clearTimeout(timeout)
  169. }
  170. timeout = setTimeout(function() {
  171. if (org_elem.is(':visible')) {
  172. tiptip_holder.stop(true, true).fadeIn(opts.fadeIn)
  173. }
  174. }, opts.delay)
  175. }
  176. function deactive_tiptip() {
  177. opts.exit.call(this);
  178. if (timeout) {
  179. clearTimeout(timeout)
  180. }
  181. tiptip_holder.fadeOut(opts.fadeOut)
  182. }
  183. }
  184. })
  185. }
  186. })(jQuery);