Skip to content

Instantly share code, notes, and snippets.

@smiler
Created March 5, 2012 13:33
Show Gist options
  • Save smiler/1978320 to your computer and use it in GitHub Desktop.
Save smiler/1978320 to your computer and use it in GitHub Desktop.
jQuery tooltip plugin
/* CSS
.tooltip-bubble {
background-color: #fffff7;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border: 1px solid #7f7f7f;
padding: 8px;
margin-top: -4px;
}
.tooltip-bubble .tooltip-arrow {
position: absolute;
bottom: -16px; left: 20px;
border: 8px solid transparent;
border-top-color: #7f7f7f;
width: 0; height: 0;
}
.tooltip-bubble .tooltip-arrow div {
border: 8px solid transparent;
border-top-color: #fffff7;
width: 0; height: 0;
margin: -9px -8px 0;
}
.tooltip-bubble-left .tooltip-arrow { left: auto; right: 20px; }
*/
(function($) {
jQuery.fn.toolTip = function(o) {
if ( this.length == 1 && typeof(o) === 'undefined' ) {
var instance = this.data('_tooltip');
if ( !instance ) instance = $(this).data('_tooltip', new jQuery.toolTip( $(this), null ));
return instance;
} else {
return this.each(function() {
$(this).data('_tooltip', new jQuery.toolTip( $(this), o ));
});
}
}
jQuery.toolTip = function( el, o ) {
this.o = jQuery.extend({
tooltipSelector: '.tooltip',
toolTopContentSelector: '>span',
toolTipElementClass: 'tooltip-bubble',
toolTipElementLeftClass: 'tooltip-bubble-left',
toolTipElementArrowClass: 'tooltip-arrow',
toolTipElementZindex: 80,
toolTipElementOffset: { x: 0, y: 0 },
toolTipCordsModifier: null
}, o);
this.toolTipElement = null;
this.offsetParent = ( this.isElementPositioned( el ) ) ? el : el.offsetParent();
this.offsetParentWidth = this.offsetParent.width();
this.offsetParentOffset = this.offsetParent.offset();
this.init();
}
jQuery.toolTip.fn = jQuery.toolTip.prototype;
jQuery.toolTip.fn.extend = jQuery.toolTip.extend = jQuery.extend;
jQuery.toolTip.fn.extend({
init: function() {
this.toolTipElement = $('<div><span></span></div>').addClass( this.o.toolTipElementClass ).append(
$('<div><div></div></div>').addClass( this.o.toolTipElementArrowClass )
).css({
position: 'absolute',
zIndex: this.o.toolTipElementZindex
}).appendTo( this.offsetParent ).hide();
var self = this, timeout;
$( this.o.tooltipSelector, this.offsetParent ).mouseenter(
function( e ) {
if ( timeout ) clearTimeout( timeout );
var el = this;
timeout = setTimeout( function() {
self.showTooltip.apply( self, [ $(el), e ] );
timeout = null;
}, 300 );
}
).mouseleave(
function( e ) {
if ( timeout ) clearTimeout( timeout );
self.hideTooltip.apply( self, [ $(this), e ] );
}
);
},
showTooltip: function( el, e ) {
var cnt_el = null;
if ( this.o.toolTopContentSelector.indexOf('#') > -1 ) {
cnt_el = $(this.o.toolTopContentSelector);
} else {
cnt_el = $(this.o.toolTopContentSelector, el);
}
this.toolTipElement.find('span').html( cnt_el.html() );
var tooltipdim = this.getElementDimentions( this.toolTipElement ), cords = { x: 0, y: 0 };
var eldim = this.getElementDimentions( el );
var eloffset = el.offset();
//Need to recalculate the parent offset as columns may have been maximised/minimised
this.offsetParentOffset = this.offsetParent.offset();
eloffset.left = eloffset.left - this.offsetParentOffset.left;
eloffset.top = eloffset.top - this.offsetParentOffset.top;
cords.x = eloffset.left;
cords.y = eloffset.top - tooltipdim.h;
if ( this.o.toolTipCordsModifier ) {
this.o.toolTipCordsModifier.apply( this, [ cords, eloffset, tooltipdim, eldim ] );
}
if ( ( cords.x + tooltipdim.w ) > this.offsetParentWidth ) {
cords.x -= (tooltipdim.w - eldim.w);
this.toolTipElement.addClass(this.o.toolTipElementLeftClass);
} else {
this.toolTipElement.removeClass(this.o.toolTipElementLeftClass);
}
this.toolTipElement.css( {
top: cords.y, left: cords.x
}).fadeIn('fast');
},
hideTooltip: function( el, e ) {
this.toolTipElement.fadeOut('fast');
},
instantHideTooltip: function( el, e ) {
this.toolTipElement.hide();
},
isElementPositioned: function( el ) {
var attr = el.css('position');
return ( attr == 'relative' || attr == 'absolute' || attr == 'fixed' );
},
getElementDimentions: function(element) {
var dim = {};
dim.w = element.outerWidth();
dim.h = element.outerHeight();
return dim;
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment