Skip to content

Instantly share code, notes, and snippets.

@zaurmag
Created August 7, 2016 18:16
Show Gist options
  • Save zaurmag/80124b9e9ccd643b38043f412b0faeab to your computer and use it in GitHub Desktop.
Save zaurmag/80124b9e9ccd643b38043f412b0faeab to your computer and use it in GitHub Desktop.
Плагин подсказки Tooltip
// ======= Tooltip =======
(function($) {
$.fn.easyTooltip = function(options) {
// default configuration properties
var defaults = {
xOffset: 10,
yOffset: 25,
tooltipId: "easyTooltip",
clickRemove: false,
content: "",
useElement: ""
};
var options = $.extend(defaults, options);
var content;
this.each(function() {
var title = $(this).attr("data-tooltip");
var bg = $(this).attr("data-tooltip-bg");
$(this).hover(function(e) {
content = (options.content != "") ? options.content : title;
content = (options.useElement != "") ? $("#" + options.useElement).html() : content;
$(this).attr("data-tooltip", "");
if (content != "" && content != undefined) {
$("body").append("<div id='" + options.tooltipId + "'>" + content + "</div>");
$("#" + options.tooltipId)
.css("position", "absolute")
.css("top", (e.pageY + options.yOffset) + "px")
.css("left", (e.pageX + options.xOffset) + "px")
.css("display", "none")
.fadeIn("fast")
}
if (bg != "" && bg != undefined) {
$('#' + options.tooltipId).addClass(bg);
}
},
function() {
$("#" + options.tooltipId).remove();
$(this).attr("data-tooltip", title);
});
$(this).mousemove(function(e) {
$("#" + options.tooltipId)
.css("top", (e.pageY + options.yOffset) + "px")
.css("left", (e.pageX + options.xOffset) + "px")
});
if (options.clickRemove) {
$(this).mouseleave(function(e) {
$("#" + options.tooltipId).remove();
$(this).attr("data-tooltip", title);
});
}
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment