$.fn.tooltip = function(options) { var defaults = { speed: 200, delay: 300, attr : false }; var opt = $.extend(defaults, options); var _tooltip = this; _tooltip.getTip = function() { return '
'; } if ($('body').find('.tooltip').length <= 0) $("body").append(_tooltip.getTip()); var tip = $('.tooltip'); _tooltip.set_timer = function(that) { that.sho_tooltip_timer = setInterval("_tooltip.show_tooltip()", defaults.delay); } _tooltip.stop_timer = function(that) { clearInterval(that.sho_tooltip_timer); } _tooltip.set_position = function(that) { var _element_position = $(that).offset(); if (_tooltip.autoNS(that) == 'n') { tip.addClass('top').css({ top: _element_position.top - (tip.innerHeight() + 5), left: _element_position.left + (that.innerWidth() / 2) - (tip.innerWidth() / 2) }) } else { tip.addClass('bottom').css({ top: _element_position.top + that.innerHeight() + 5 , left: _element_position.left + (that.innerWidth() / 2) - (tip.innerWidth() / 2) }) } } _tooltip.autoNS = function(that) { return $(that).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 'n' : 's' } _tooltip.update = function(that) { var tooltip_content = ($(that).attr('tooltip') || $(that).attr('ititle')); if (opt.attr) { var tooltip_content = $(that).attr(opt.attr); } if (tooltip_content == undefined) { _tooltip.destroy(); tooltip_content = ''; } else { tip.html(''+tooltip_content+''); _tooltip.set_position(that); tip.removeClass('hide') } return tooltip_content; } _tooltip.destroy = function(that) { $(that).off('mouseover mouseleave'); tip.empty().addClass('hide') } _tooltip.show_tooltip = function() { tip.removeClass('hide').addClass('show') } if (typeof options == 'undefined' || typeof options == 'object') { return _tooltip.each(function() { var _this = $(this); if (opt.attr) { _this.attr('tooltip', function() { return $(this).attr(opt.attr); }).removeAttr(opt.attr) } var tooltip_content = (_this.attr('tooltip') || _this.attr('ititle')); _this.on('mouseover',function() { if (tooltip_content != '' && tooltip_content != undefined) { tip.html(''+tooltip_content+''); _tooltip.set_position(_this); _tooltip.show_tooltip(); } }) _this.on('mouseleave',function() { tip.removeClass('show top bottom') }); _this.on('update_tooltip',function() { tooltip_content = _tooltip.update(_this); }); _this.on('destroy_tooltip',function() { _tooltip.update(_this); }); }); } else { switch (options) { case 'update': _tooltip.trigger('update_tooltip') break; case 'destroy': break; } } };