forked from ellimccale/jquery-style-my-tooltips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.style-my-tooltips.js
193 lines (130 loc) · 4.77 KB
/
jquery.style-my-tooltips.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// style-my-tootltips by malihu (http://manos.malihu.gr)
// plugin home http://manos.malihu.gr/style-my-tooltips-jquery-plugin
(function($) {
var methods = {
init: function(options) {
var defaults = {
tip_follows_cursor: true, // tooltip follows cursor: boolean
tip_delay_time: 100, // tooltip delay before displaying: milliseconds
tip_fade_speed: 300, // tooltip fade in/out speed: milliseconds
attribute: "title", // tooltip text come from this attribute
};
options = $.extend(defaults, options);
if (!$("#s-m-t-tooltip").length) {
$("body").append("<div id='s-m-t-tooltip'><div></div></div>");
}
var $smtTooltip = $("#s-m-t-tooltip");
$smtTooltip.css({
"position": "absolute",
"display": "none",
}).data("smt-z-index", $smtTooltip.css("z-index")).children("div").css({
"width": "100%",
"height": "100%",
});
function smtGetCursorCoords(event) {
var smtCursorCoordsX = event.pageX;
var smtCursorCoordsY = event.pageY;
$smtTooltip.style_my_tooltips("position", {
smtCursorCoordsX: smtCursorCoordsX,
smtCursorCoordsY: smtCursorCoordsY,
});
}
$(document).on("mouseleave mousedown click", ".smt-current-element", function() {
var $this = $(this);
clearTimeout(smtTooltip_delay);
$smtTooltip.style_my_tooltips("hide", {
speed: $this.data("smt-fade-speed"),
});
$(document).unbind("mousemove", this.tip_mousemove);
this.tip_mousemove = undefined;
$this.removeClass("smt-current-element");
if ($this.attr(options.attribute) === "") {
$this.attr(options.attribute, $this.data("smt-title"));
}
});
return this.on("mouseenter", function(event) {
var $this = $(this);
var title = $this.attr(options.attribute);
$this.addClass("smt-current-element").data({
"smt-title": title,
"smt-fade-speed": options.tip_fade_speed,
}).attr(options.attribute, "");
$smtTooltip.style_my_tooltips("update", {
title: title,
speed: options.tip_fade_speed,
delay: options.tip_delay_time,
tip_follows_cursor: options.tip_follows_cursor,
});
if (!this.tip_mousemove) {
this.tip_mousemove = function(event) {
smtGetCursorCoords(event);
};
$(document).bind("mousemove", this.tip_mousemove);
}
});
},
update: function(options) {
var $this = $(this);
$this.stop().css({
"display": "none",
"z-index": $this.data("smt-z-index"),
}).children("div").text(options.title);
smtTooltip_delay = setTimeout(function() {
$this.style_my_tooltips("show", {
speed: options.speed,
tip_follows_cursor: options.tip_follows_cursor,
});
}, options.delay);
},
show: function(options) {
var $this = $(this);
$this.stop().fadeTo(options.speed, 1);
if (!options.tip_follows_cursor) {
$(document).unbind("mousemove", this.tip_mousemove);
this.tip_mousemove = undefined;
}
},
hide: function(options) {
var $this = $(this);
$this.stop().fadeTo(options.speed, 0);
},
position: function(options) {
var $this = $(this);
var winScrollX = $(window).scrollLeft();
var winScrollY = $(window).scrollTop();
var tipWidth = $this.outerWidth(true);
var tipHeight = $this.outerHeight(true);
var leftOffset = (options.smtCursorCoordsX + tipWidth) - winScrollX;
var topOffset = (options.smtCursorCoordsY + tipHeight) - winScrollY;
if (leftOffset <= $(window).width() && leftOffset <= $(document).width()) {
$this.css("left", options.smtCursorCoordsX);
} else {
var thePosX = options.smtCursorCoordsX - tipWidth;
if (thePosX >= winScrollX) {
$this.css("left", thePosX);
} else {
$this.css("left", winScrollX);
}
}
if (topOffset <= $(window).height() && topOffset <= $(document).height()) {
$this.css("top", options.smtCursorCoordsY);
} else {
var thePosY = options.smtCursorCoordsY - tipHeight;
if (thePosY >= winScrollY) {
$this.css("top", thePosY);
} else {
$this.css("top", winScrollY);
}
}
},
};
$.fn.style_my_tooltips = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === "object" || !method) {
return methods.init.apply(this, arguments);
} else {
$.error("Method " + method + " does not exist");
}
};
})(jQuery);