jQuery.fn.quicktip = function(options){
var defaults = {
speed: 500,
xOffset: 26,
yOffset: -5
};
var options = $.extend(defaults, options);
return this.each(function(){
var $this = jQuery(this)
//Pass the title to a variable and then remove it from DOM
if ($this.attr('title') != ''){
var tipTitle = ($this.attr('title'));
}else{
var tipTitle = 'Quick tip';
}
//Remove title attribute
$this.removeAttr('title');
$(this).hover(function(e){
$(this).css('cursor', 'pointer')
$("body").append("
" + tipTitle + "
");
$("#tooltip")
.css("top", (e.pageY - $("#tooltip").outerHeight() - 10) + "px") // позиция сверху курсора с отступом
.css("left", (e.pageX + defaults.xOffset) + "px")
.fadeIn(options.speed);
}, function() {
//Remove the tooltip from the DOM
$("#tooltip").remove();
});
$(this).mousemove(function(e) {
if ($("#tooltip").length) {
var tooltipHeight = $("#tooltip").outerHeight();
var tooltipWidth = $("#tooltip").outerWidth();
var topPosition = e.pageY - tooltipHeight - 10; // позиция сверху с отступом
var leftPosition = e.pageX + defaults.xOffset;
// Можно добавить проверку границ окна, чтобы подсказка не выходила за пределы
if (topPosition < 0) {
topPosition = e.pageY + defaults.yOffset; // если слишком высоко, показываем снизу
}
if (leftPosition + tooltipWidth > $(window).width()) {
leftPosition = e.pageX - tooltipWidth - defaults.xOffset; // показываем слева
}
$("#tooltip")
.css("top", topPosition + "px")
.css("left", leftPosition + "px");
}
});
});
};
// Инициализация
$(document).ready(function(){
$('a[title]').quicktip({ speed:700 });
$('img[title]').quicktip({ speed:700 });
$('div[title]').quicktip({ speed:700 });
$('span[title]').quicktip({ speed:700 });
});