jquery.tiptip.min.js 9.1 KB

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