-
Notifications
You must be signed in to change notification settings - Fork 9
/
jquery.ezpz_tooltip.js
174 lines (138 loc) · 5.51 KB
/
jquery.ezpz_tooltip.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
// EZPZ Tooltip v1.0; Copyright (c) 2009 Mike Enriquez, http://theezpzway.com; Released under the MIT License
(function($){
$.fn.ezpz_tooltip = function(options){
var settings = $.extend({}, $.fn.ezpz_tooltip.defaults, options);
return this.each(function(){
var content = $("#" + getContentId(this.id));
var targetMousedOver = $(this).mouseover(function(){
settings.beforeShow(content, $(this));
}).mousemove(function(e){
var contentInfo = getElementDimensionsAndPosition(content);
var targetInfo = getElementDimensionsAndPosition($(this));
var contentInfo = $.fn.ezpz_tooltip.positions[settings.contentPosition](contentInfo, e.pageX, e.pageY, settings.offset, targetInfo);
var contentInfo = keepInWindow(contentInfo);
content.css('top', contentInfo['top']);
content.css('left', contentInfo['left']);
settings.showContent(content);
});
if (settings.stayOnContent && this.id != "") {
$("#" + this.id + ", #" + getContentId(this.id)).mouseover(function(){
content.css('display', 'block');
}).mouseout(function(){
content.css('display', 'none');
settings.afterHide();
});
}
else {
targetMousedOver.mouseout(function(){
settings.hideContent(content);
settings.afterHide();
})
}
});
function getContentId(targetId){
if (settings.contentId == "") {
var name = targetId.split('-')[0];
var id = targetId.split('-')[2];
return name + '-content-' + id;
}
else {
return settings.contentId;
}
};
function getElementDimensionsAndPosition(element){
var height = element.outerHeight(true);
var width = element.outerWidth(true);
var top = $(element).offset().top;
var left = $(element).offset().left;
var info = new Array();
// Set dimensions
info['height'] = height;
info['width'] = width;
// Set position
info['top'] = top;
info['left'] = left;
return info;
};
function keepInWindow(contentInfo){
var windowWidth = $(window).width();
var windowTop = $(window).scrollTop();
var output = new Array();
output = contentInfo;
if (contentInfo['top'] < windowTop) { // Top edge is too high
output['top'] = windowTop;
}
if ((contentInfo['left'] + contentInfo['width']) > windowWidth) { // Right edge is past the window
output['left'] = windowWidth - contentInfo['width'];
}
if (contentInfo['left'] < 0) { // Left edge is too far left
output['left'] = 0;
}
return output;
};
};
$.fn.ezpz_tooltip.positionContent = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
contentInfo['top'] = mouseY - offset - contentInfo['height'];
contentInfo['left'] = mouseX + offset;
return contentInfo;
};
$.fn.ezpz_tooltip.positions = {
aboveRightFollow: function(contentInfo, mouseX, mouseY, offset, targetInfo) {
contentInfo['top'] = mouseY - offset - contentInfo['height'];
contentInfo['left'] = mouseX + offset;
return contentInfo;
}
};
$.fn.ezpz_tooltip.defaults = {
contentPosition: 'aboveRightFollow',
stayOnContent: false,
offset: 10,
contentId: "",
beforeShow: function(content){},
showContent: function(content){
content.show();
},
hideContent: function(content){
content.hide();
},
afterHide: function(){}
};
})(jQuery);
// Plugin for different content positions. Keep what you need, remove what you don't need to reduce file size.
(function($){
$.fn.ezpz_tooltip.positions.aboveFollow = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
contentInfo['top'] = mouseY - offset - contentInfo['height'];
contentInfo['left'] = mouseX - (contentInfo['width'] / 2);
return contentInfo;
};
$.fn.ezpz_tooltip.positions.rightFollow = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
contentInfo['top'] = mouseY - (contentInfo['height'] / 2);
contentInfo['left'] = mouseX + offset;
return contentInfo;
};
$.fn.ezpz_tooltip.positions.belowRightFollow = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
contentInfo['top'] = mouseY + offset;
contentInfo['left'] = mouseX + offset;
return contentInfo;
};
$.fn.ezpz_tooltip.positions.belowFollow = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
contentInfo['top'] = mouseY + offset;
contentInfo['left'] = mouseX - (contentInfo['width'] / 2);
return contentInfo;
};
$.fn.ezpz_tooltip.positions.aboveStatic = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
contentInfo['top'] = targetInfo['top'] - offset - contentInfo['height'];
contentInfo['left'] = (targetInfo['left'] + (targetInfo['width'] / 2)) - (contentInfo['width'] / 2);
return contentInfo;
};
$.fn.ezpz_tooltip.positions.rightStatic = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
contentInfo['top'] = (targetInfo['top'] + (targetInfo['height'] / 2)) - (contentInfo['height'] / 2);
contentInfo['left'] = targetInfo['left'] + targetInfo['width'] + offset;
return contentInfo;
};
$.fn.ezpz_tooltip.positions.belowStatic = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
contentInfo['top'] = targetInfo['top'] + targetInfo['height'] + offset;
contentInfo['left'] = (targetInfo['left'] + (targetInfo['width'] / 2)) - (contentInfo['width'] / 2);
return contentInfo;
};
})(jQuery);