Here is a pretty simple way to do this:
var timeout; function hide() { timeout = setTimeout(function () { $("#tooltip").hide('fast'); }, 500); }; $("#tip").mouseover(function () { clearTimeout(timeout); $("#tooltip").stop().show('fast'); }).mouseout(hide); $("#tooltip").mouseover(function () { clearTimeout(timeout); }).mouseout(hide);
Where #tip is the element you want to #tip mouse over so that the tooltip appears, and #tooltip is the actual tooltip.
Here is an example: http://jsfiddle.net/pvyhY/
If you want to wrap this in a jQuery plugin:
(function($) { $.fn.tooltip = function(tooltipEl) { var $tooltipEl = $(tooltipEl); return this.each(function() { var $this = $(this); var hide = function () { var timeout = setTimeout(function () { $tooltipEl.hide(); }, 500); $this.data("tooltip.timeout", timeout); }; $this.hover(function () { clearTimeout($this.data("tooltip.timeout")); $tooltipEl.show(); }, hide); $tooltipEl.hover(function () { clearTimeout($this.data("tooltip.timeout")); }, hide); }); }; })(jQuery);
Using:
$(document).ready(function() { $("#tip").tooltip("#tooltip"); });
Add more functionality and you end up with a great qTip plugin , which I also recommend taking a look at.
source share