| 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 |
|
| 22 |
(function($){ |
| 23 |
$.fn.tipTip = function(options) { |
| 24 |
var defaults = { |
| 25 |
activation: "click", |
| 26 |
keepAlive: false, |
| 27 |
maxWidth: "200px", |
| 28 |
edgeOffset: 8, |
| 29 |
defaultPosition: "right", |
| 30 |
delay: 400, |
| 31 |
fadeIn: 200, |
| 32 |
fadeOut: 200, |
| 33 |
attribute: "title", |
| 34 |
content: false, // HTML or String to fill TipTIp with |
| 35 |
enter: function(){}, |
| 36 |
exit: function(){} |
| 37 |
}; |
| 38 |
var opts = $.extend( defaults, options ); |
| 39 |
|
| 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>' ) ) ); |
| 46 |
} else { |
| 47 |
var tiptip_holder = $( "#tiptip_holder" ); |
| 48 |
var tiptip_content = $( "#tiptip_content" ); |
| 49 |
var tiptip_arrow = $( "#tiptip_arrow" ); |
| 50 |
} |
| 51 |
|
| 52 |
return this.each( |
| 53 |
function(){ |
| 54 |
var org_elem = $( this ); |
| 55 |
if (opts.content) { |
| 56 |
var org_title = opts.content; |
| 57 |
} else { |
| 58 |
var org_title = org_elem.attr( opts.attribute ); |
| 59 |
} |
| 60 |
|
| 61 |
if (org_title != "") { |
| 62 |
if ( ! opts.content) { |
| 63 |
org_elem.removeAttr( opts.attribute ); //remove original Attribute |
| 64 |
} |
| 65 |
var timeout = false; |
| 66 |
|
| 67 |
if (opts.activation == "hover") { |
| 68 |
org_elem.hover( |
| 69 |
function(){ |
| 70 |
active_tiptip(); |
| 71 |
}, |
| 72 |
function(){ |
| 73 |
if ( ! opts.keepAlive) { |
| 74 |
deactive_tiptip(); |
| 75 |
} |
| 76 |
} |
| 77 |
); |
| 78 |
if (opts.keepAlive) { |
| 79 |
tiptip_holder.hover( |
| 80 |
function(){}, |
| 81 |
function(){ |
| 82 |
deactive_tiptip(); |
| 83 |
} |
| 84 |
); |
| 85 |
} |
| 86 |
} else if (opts.activation == "focus") { |
| 87 |
org_elem.focus( |
| 88 |
function(){ |
| 89 |
active_tiptip(); |
| 90 |
} |
| 91 |
).blur( |
| 92 |
function(){ |
| 93 |
deactive_tiptip(); |
| 94 |
} |
| 95 |
); |
| 96 |
} else if (opts.activation == "click") { |
| 97 |
org_elem.click( |
| 98 |
function(){ |
| 99 |
active_tiptip(); |
| 100 |
return false; |
| 101 |
} |
| 102 |
).hover( |
| 103 |
function(){}, |
| 104 |
function(){ |
| 105 |
if ( ! opts.keepAlive) { |
| 106 |
deactive_tiptip(); |
| 107 |
} |
| 108 |
} |
| 109 |
); |
| 110 |
if (opts.keepAlive) { |
| 111 |
tiptip_holder.hover( |
| 112 |
function(){}, |
| 113 |
function(){ |
| 114 |
deactive_tiptip(); |
| 115 |
} |
| 116 |
); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
function active_tiptip(){ |
| 121 |
opts.enter.call( this ); |
| 122 |
tiptip_content.html( org_title ); |
| 123 |
tiptip_holder.hide().removeAttr( "class" ).css( "margin","0" ); |
| 124 |
tiptip_arrow.removeAttr( "style" ); |
| 125 |
|
| 126 |
var top = parseInt( org_elem.offset()['top'] ); |
| 127 |
var left = parseInt( org_elem.offset()['left'] ); |
| 128 |
var org_width = parseInt( org_elem.outerWidth() ); |
| 129 |
var org_height = parseInt( org_elem.outerHeight() ); |
| 130 |
var tip_w = tiptip_holder.outerWidth(); |
| 131 |
var tip_h = tiptip_holder.outerHeight(); |
| 132 |
var w_compare = Math.round( (org_width - tip_w) / 2 ); |
| 133 |
var h_compare = Math.round( (org_height - tip_h) / 2 ); |
| 134 |
var marg_left = Math.round( left + w_compare ); |
| 135 |
var marg_top = Math.round( top + org_height + opts.edgeOffset ); |
| 136 |
var t_class = ""; |
| 137 |
var arrow_top = ""; |
| 138 |
var arrow_left = Math.round( tip_w - 12 ) / 2; |
| 139 |
|
| 140 |
if (opts.defaultPosition == "bottom") { |
| 141 |
t_class = "_bottom"; |
| 142 |
} else if (opts.defaultPosition == "top") { |
| 143 |
t_class = "_top"; |
| 144 |
} else if (opts.defaultPosition == "left") { |
| 145 |
t_class = "_left"; |
| 146 |
} else if (opts.defaultPosition == "right") { |
| 147 |
t_class = "_right"; |
| 148 |
} |
| 149 |
|
| 150 |
var right_compare = (w_compare + left) < parseInt( $( window ).scrollLeft() ); |
| 151 |
var left_compare = (tip_w + left) > parseInt( $( window ).width() ); |
| 152 |
|
| 153 |
if ((right_compare && w_compare < 0) || (t_class == "_right" && ! left_compare) || (t_class == "_left" && left < (tip_w + opts.edgeOffset + 5))) { |
| 154 |
t_class = "_right"; |
| 155 |
arrow_top = Math.round( tip_h - 13 ) / 2; |
| 156 |
arrow_left = -12; |
| 157 |
marg_left = Math.round( left + org_width + opts.edgeOffset ); |
| 158 |
marg_top = Math.round( top + h_compare ); |
| 159 |
} else if ((left_compare && w_compare < 0) || (t_class == "_left" && ! right_compare)) { |
| 160 |
t_class = "_left"; |
| 161 |
arrow_top = Math.round( tip_h - 13 ) / 2; |
| 162 |
arrow_left = Math.round( tip_w ); |
| 163 |
marg_left = Math.round( left - (tip_w + opts.edgeOffset + 5) ); |
| 164 |
marg_top = Math.round( top + h_compare ); |
| 165 |
} |
| 166 |
|
| 167 |
var top_compare = (top + org_height + opts.edgeOffset + tip_h + 8) > parseInt( $( window ).height() + $( window ).scrollTop() ); |
| 168 |
var bottom_compare = ((top + org_height) - (opts.edgeOffset + tip_h + 8)) < 0; |
| 169 |
|
| 170 |
if (top_compare || (t_class == "_bottom" && top_compare) || (t_class == "_top" && ! bottom_compare)) { |
| 171 |
if (t_class == "_top" || t_class == "_bottom") { |
| 172 |
t_class = "_top"; |
| 173 |
} else { |
| 174 |
t_class = t_class + "_top"; |
| 175 |
} |
| 176 |
arrow_top = tip_h; |
| 177 |
marg_top = Math.round( top - (tip_h + 5 + opts.edgeOffset) ); |
| 178 |
} else if (bottom_compare | (t_class == "_top" && bottom_compare) || (t_class == "_bottom" && ! top_compare)) { |
| 179 |
if (t_class == "_top" || t_class == "_bottom") { |
| 180 |
t_class = "_bottom"; |
| 181 |
} else { |
| 182 |
t_class = t_class + "_bottom"; |
| 183 |
} |
| 184 |
arrow_top = -12; |
| 185 |
marg_top = Math.round( top + org_height + opts.edgeOffset ); |
| 186 |
} |
| 187 |
|
| 188 |
if (t_class == "_right_top" || t_class == "_left_top") { |
| 189 |
marg_top = marg_top + 5; |
| 190 |
} else if (t_class == "_right_bottom" || t_class == "_left_bottom") { |
| 191 |
marg_top = marg_top - 5; |
| 192 |
} |
| 193 |
if (t_class == "_left_top" || t_class == "_left_bottom") { |
| 194 |
marg_left = marg_left + 5; |
| 195 |
} |
| 196 |
|
| 197 |
tiptip_arrow.css( {"margin-left": arrow_left + "px", "margin-top": arrow_top + "px"} ); |
| 198 |
tiptip_holder.css( {"margin-left": marg_left + "px", "margin-top": marg_top + "px"} ).attr( "class","tip" + t_class ); |
| 199 |
|
| 200 |
if (timeout) { |
| 201 |
clearTimeout( timeout ); } |
| 202 |
timeout = setTimeout( function(){ tiptip_holder.stop( true,true ).fadeIn( opts.fadeIn ); }, opts.delay ); |
| 203 |
} |
| 204 |
|
| 205 |
function deactive_tiptip(){ |
| 206 |
opts.exit.call( this ); |
| 207 |
if (timeout) { |
| 208 |
clearTimeout( timeout ); } |
| 209 |
tiptip_holder.fadeOut( opts.fadeOut ); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
); |
| 214 |
} |
| 215 |
})( jQuery ); |
| 216 |
|