3 * Copyright 2010 Drew Wilson
5 * code.drewwilson.com/entry/tiptip-jquery-plugin
7 * Version 1.3 - Updated: Mar. 23, 2010
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.
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
23 $.fn.tipTip = function(options) {
29 defaultPosition: "bottom",
34 content: false, // HTML or String to fill TipTIp with
38 var opts = $.extend(defaults, options);
40 // Setup tip tip elements and render them to the DOM
41 if($("#tiptip_holder").length <= 0){
42 var tiptip_holder = $('<div id="tiptip_holder" style="max-width:'+ opts.maxWidth +';"></div>');
43 var tiptip_content = $('<div id="tiptip_content"></div>');
44 var tiptip_arrow = $('<div id="tiptip_arrow"></div>');
45 $("body").append(tiptip_holder.html(tiptip_content).prepend(tiptip_arrow.html('<div id="tiptip_arrow_inner"></div>')));
47 var tiptip_holder = $("#tiptip_holder");
48 var tiptip_content = $("#tiptip_content");
49 var tiptip_arrow = $("#tiptip_arrow");
52 return this.each(function(){
53 var org_elem = $(this);
55 var org_title = opts.content;
57 var org_title = org_elem.attr(opts.attribute);
59 if(org_title && org_title != ""){
61 org_elem.removeAttr(opts.attribute); //remove original Attribute
65 if(opts.activation == "hover"){
66 org_elem.hover(function(){
74 tiptip_holder.hover(function(){}, function(){
78 } else if(opts.activation == "focus"){
79 org_elem.focus(function(){
84 } else if(opts.activation == "click"){
85 org_elem.click(function(){
88 }).hover(function(){},function(){
94 tiptip_holder.hover(function(){}, function(){
100 function active_tiptip(){
101 opts.enter.call(this);
102 tiptip_content.html(org_title);
103 tiptip_holder.hide().removeAttr("class").css("margin","0");
104 tiptip_arrow.removeAttr("style");
106 var top = parseInt(org_elem.offset()['top']);
107 var left = parseInt(org_elem.offset()['left']);
108 var org_width = parseInt(org_elem.outerWidth());
109 var org_height = parseInt(org_elem.outerHeight());
110 var tip_w = tiptip_holder.outerWidth();
111 var tip_h = tiptip_holder.outerHeight();
112 var w_compare = Math.round((org_width - tip_w) / 2);
113 var h_compare = Math.round((org_height - tip_h) / 2);
114 var marg_left = Math.round(left + w_compare);
115 var marg_top = Math.round(top + org_height + opts.edgeOffset);
118 var arrow_left = Math.round(tip_w - 12) / 2;
120 if(opts.defaultPosition == "bottom"){
122 } else if(opts.defaultPosition == "top"){
124 } else if(opts.defaultPosition == "left"){
126 } else if(opts.defaultPosition == "right"){
130 var right_compare = (w_compare + left) < parseInt($(window).scrollLeft());
131 var left_compare = (tip_w + left) > parseInt($(window).width());
133 if((right_compare && w_compare < 0) || (t_class == "_right" && !left_compare) || (t_class == "_left" && left < (tip_w + opts.edgeOffset + 5))){
135 arrow_top = Math.round(tip_h - 13) / 2;
137 marg_left = Math.round(left + org_width + opts.edgeOffset);
138 marg_top = Math.round(top + h_compare);
139 } else if((left_compare && w_compare < 0) || (t_class == "_left" && !right_compare)){
141 arrow_top = Math.round(tip_h - 13) / 2;
142 arrow_left = Math.round(tip_w);
143 marg_left = Math.round(left - (tip_w + opts.edgeOffset + 5));
144 marg_top = Math.round(top + h_compare);
147 var top_compare = (top + org_height + opts.edgeOffset + tip_h + 8) > parseInt($(window).height() + $(window).scrollTop());
148 var bottom_compare = ((top + org_height) - (opts.edgeOffset + tip_h + 8)) < 0;
150 if(top_compare || (t_class == "_bottom" && top_compare) || (t_class == "_top" && !bottom_compare)){
151 if(t_class == "_top" || t_class == "_bottom"){
154 t_class = t_class+"_top";
157 marg_top = Math.round(top - (tip_h + 5 + opts.edgeOffset));
158 } else if(bottom_compare | (t_class == "_top" && bottom_compare) || (t_class == "_bottom" && !top_compare)){
159 if(t_class == "_top" || t_class == "_bottom"){
162 t_class = t_class+"_bottom";
165 marg_top = Math.round(top + org_height + opts.edgeOffset);
168 if(t_class == "_right_top" || t_class == "_left_top"){
169 marg_top = marg_top + 5;
170 } else if(t_class == "_right_bottom" || t_class == "_left_bottom"){
171 marg_top = marg_top - 5;
173 if(t_class == "_left_top" || t_class == "_left_bottom"){
174 marg_left = marg_left + 5;
176 tiptip_arrow.css({"margin-left": arrow_left+"px", "margin-top": arrow_top+"px"});
177 tiptip_holder.css({"margin-left": marg_left+"px", "margin-top": marg_top+"px"}).attr("class","tip"+t_class);
179 if (timeout){ clearTimeout(timeout); }
180 timeout = setTimeout(function(){ tiptip_holder.stop(true,true).fadeIn(opts.fadeIn); }, opts.delay);
183 function deactive_tiptip(){
184 opts.exit.call(this);
185 if (timeout){ clearTimeout(timeout); }
186 tiptip_holder.fadeOut(opts.fadeOut);