Created
February 2, 2012 06:49
-
-
Save leetrout/1722066 to your computer and use it in GitHub Desktop.
YATT - Yet Another Tool Tip for jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
'use strict'; | |
$.fn.tooltip = function (options) { | |
return this.each(function (el) { | |
var settings = $.extend({ | |
$element: $('<div class="tooltip_wrapper" />'), | |
eventName: 'hover', | |
offset: {top: 0, left: 0}, | |
preventDefault: true | |
}, options), | |
$target = $(this); | |
function positionElement() { | |
// posistions the element next to the target | |
var pos = $target.position(), | |
ow = $target.outerWidth(); | |
settings.$element.css({ | |
position: 'absolute', | |
top: (pos.top + settings.offset.top) + 'px', | |
left: (pos.left + ow + settings.offset.left) + 'px', | |
"z-index": 99999 | |
}); | |
} | |
function toggleToolTip(e) { | |
// toggles the tooltip | |
if (settings.preventDefault) { | |
e.preventDefault(); | |
} | |
if (!settings.$element.is(':visible')) { | |
positionElement(); | |
} | |
settings.$element.toggle(); | |
} | |
// if the $element doesn't have a parent insert it after the target | |
// and hide it | |
if (settings.$element.parent().length === 0) { | |
settings.$element.insertAfter($target).hide(); | |
} | |
// bind provided event name to toggle | |
$target.bind(settings.eventName + '.tooltip', toggleToolTip); | |
// bind any element children with class close_tt (close tool tip) | |
// to toggle | |
settings.$element.find('.close_tt').bind('click.tooltip', toggleToolTip); | |
}); | |
}; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment