Skip to content

Instantly share code, notes, and snippets.

@rpheath
Created February 18, 2009 20:20
Show Gist options
  • Save rpheath/66517 to your computer and use it in GitHub Desktop.
Save rpheath/66517 to your computer and use it in GitHub Desktop.
// tooltip plugin for jQuery
(function($) {
$.fn.tooltip = function(options) {
var options = $.extend({
xOffset: 30,
yOffset: 7,
follow: false
}, options)
return this.each(function() {
$(this).hover(function(e) {
// prevent duplicate "hovers"
this._title = this.title
this.title = ''
$('body').append('<p id="tooltip">' + this._title + '</p>')
$('#tooltip').
css('top', (e.pageY - options.yOffset) + 'px').
css('left', (e.pageX + options.xOffset) + 'px').
fadeIn('fast')
}, function() {
this.title = this._title
$('#tooltip').remove()
})
// supports the option to follow the
// tooltip wherever the cursor goes
if (options.follow) {
$(this).mousemove(function(e) {
$('#tooltip').
css('top', (e.pageY - options.yOffset) + 'px').
css('left', (e.pageX + options.xOffset) + 'px')
})
}
})
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment