74249687
Luigi Serra
Cross browser con...
|
1
2
3
|
(function ($) {
$.fn.tooltip = function (options) {
var timeout = null,
|
74249687
Luigi Serra
Cross browser con...
|
4
5
6
7
8
9
10
11
12
13
14
|
margin = 5;
// Defaults
var defaults = {
delay: 350
};
// Remove tooltip from the activator
if (options === "remove") {
this.each(function(){
$('#' + $(this).attr('data-tooltip-id')).remove();
|
a1a3bc73
Luigi Serra
graphs updates
|
15
|
$(this).off('mouseenter.tooltip mouseleave.tooltip');
|
74249687
Luigi Serra
Cross browser con...
|
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
|
});
return false;
}
options = $.extend(defaults, options);
return this.each(function(){
var tooltipId = Materialize.guid();
var origin = $(this);
origin.attr('data-tooltip-id', tooltipId);
// Create Text span
var tooltip_text = $('<span></span>').text(origin.attr('data-tooltip'));
// Create tooltip
var newTooltip = $('<div></div>');
newTooltip.addClass('material-tooltip').append(tooltip_text)
.appendTo($('body'))
.attr('id', tooltipId);
var backdrop = $('<div></div>').addClass('backdrop');
backdrop.appendTo(newTooltip);
backdrop.css({ top: 0, left:0 });
|
a1a3bc73
Luigi Serra
graphs updates
|
42
|
//Destroy previously binded events
|
74249687
Luigi Serra
Cross browser con...
|
43
|
origin.off('mouseenter.tooltip mouseleave.tooltip');
|
a1a3bc73
Luigi Serra
graphs updates
|
44
45
|
// Mouse In
var started = false, timeoutRef;
|
74249687
Luigi Serra
Cross browser con...
|
46
47
|
origin.on({
'mouseenter.tooltip': function(e) {
|
a1a3bc73
Luigi Serra
graphs updates
|
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
var tooltip_delay = origin.attr('data-delay');
tooltip_delay = (tooltip_delay === undefined || tooltip_delay === '') ?
options.delay : tooltip_delay;
timeoutRef = setTimeout(function(){
started = true;
newTooltip.velocity('stop');
backdrop.velocity('stop');
newTooltip.css({ display: 'block', left: '0px', top: '0px' });
// Set Tooltip text
newTooltip.children('span').text(origin.attr('data-tooltip'));
// Tooltip positioning
var originWidth = origin.outerWidth();
var originHeight = origin.outerHeight();
var tooltipPosition = origin.attr('data-position');
var tooltipHeight = newTooltip.outerHeight();
var tooltipWidth = newTooltip.outerWidth();
var tooltipVerticalMovement = '0px';
var tooltipHorizontalMovement = '0px';
var scale_factor = 8;
var targetTop, targetLeft, newCoordinates;
if (tooltipPosition === "top") {
|
74249687
Luigi Serra
Cross browser con...
|
72
|
// Top Position
|
a1a3bc73
Luigi Serra
graphs updates
|
73
74
75
76
|
targetTop = origin.offset().top - tooltipHeight - margin;
targetLeft = origin.offset().left + originWidth/2 - tooltipWidth/2;
newCoordinates = repositionWithinScreen(targetLeft, targetTop, tooltipWidth, tooltipHeight);
|
74249687
Luigi Serra
Cross browser con...
|
77
78
79
80
81
82
|
tooltipVerticalMovement = '-10px';
backdrop.css({
borderRadius: '14px 14px 0 0',
transformOrigin: '50% 90%',
marginTop: tooltipHeight,
marginLeft: (tooltipWidth/2) - (backdrop.width()/2)
|
a1a3bc73
Luigi Serra
graphs updates
|
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
});
}
// Left Position
else if (tooltipPosition === "left") {
targetTop = origin.offset().top + originHeight/2 - tooltipHeight/2;
targetLeft = origin.offset().left - tooltipWidth - margin;
newCoordinates = repositionWithinScreen(targetLeft, targetTop, tooltipWidth, tooltipHeight);
tooltipHorizontalMovement = '-10px';
backdrop.css({
width: '14px',
height: '14px',
borderRadius: '14px 0 0 14px',
transformOrigin: '95% 50%',
marginTop: tooltipHeight/2,
marginLeft: tooltipWidth
});
}
// Right Position
else if (tooltipPosition === "right") {
targetTop = origin.offset().top + originHeight/2 - tooltipHeight/2;
targetLeft = origin.offset().left + originWidth + margin;
newCoordinates = repositionWithinScreen(targetLeft, targetTop, tooltipWidth, tooltipHeight);
|
74249687
Luigi Serra
Cross browser con...
|
106
|
|
a1a3bc73
Luigi Serra
graphs updates
|
107
108
109
110
111
112
113
114
|
tooltipHorizontalMovement = '+10px';
backdrop.css({
width: '14px',
height: '14px',
borderRadius: '0 14px 14px 0',
transformOrigin: '5% 50%',
marginTop: tooltipHeight/2,
marginLeft: '0px'
|
74249687
Luigi Serra
Cross browser con...
|
115
|
});
|
a1a3bc73
Luigi Serra
graphs updates
|
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
|
}
else {
// Bottom Position
targetTop = origin.offset().top + origin.outerHeight() + margin;
targetLeft = origin.offset().left + originWidth/2 - tooltipWidth/2;
newCoordinates = repositionWithinScreen(targetLeft, targetTop, tooltipWidth, tooltipHeight);
tooltipVerticalMovement = '+10px';
backdrop.css({
marginLeft: (tooltipWidth/2) - (backdrop.width()/2)
});
}
// Set tooptip css placement
newTooltip.css({
top: newCoordinates.y,
left: newCoordinates.x
});
// Calculate Scale to fill
scale_factor = tooltipWidth / 8;
if (scale_factor < 8) {
scale_factor = 8;
}
if (tooltipPosition === "right" || tooltipPosition === "left") {
scale_factor = tooltipWidth / 10;
if (scale_factor < 6)
scale_factor = 6;
}
newTooltip.velocity({ marginTop: tooltipVerticalMovement, marginLeft: tooltipHorizontalMovement}, { duration: 350, queue: false })
.velocity({opacity: 1}, {duration: 300, delay: 50, queue: false});
backdrop.css({ display: 'block' })
|
74249687
Luigi Serra
Cross browser con...
|
148
149
150
|
.velocity({opacity:1},{duration: 55, delay: 0, queue: false})
.velocity({scale: scale_factor}, {duration: 300, delay: 0, queue: false, easing: 'easeInOutQuad'});
|
a1a3bc73
Luigi Serra
graphs updates
|
151
152
|
}, tooltip_delay); // End Interval
|
74249687
Luigi Serra
Cross browser con...
|
153
154
155
156
157
|
// Mouse Out
},
'mouseleave.tooltip': function(){
// Reset State
|
a1a3bc73
Luigi Serra
graphs updates
|
158
159
|
started = false;
clearTimeout(timeoutRef);
|
74249687
Luigi Serra
Cross browser con...
|
160
161
|
// Animate back
|
a1a3bc73
Luigi Serra
graphs updates
|
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
setTimeout(function() {
if (started != true) {
newTooltip.velocity({
opacity: 0, marginTop: 0, marginLeft: 0}, { duration: 225, queue: false});
backdrop.velocity({opacity: 0, scale: 1}, {
duration:225,
queue: false,
complete: function(){
backdrop.css('display', 'none');
newTooltip.css('display', 'none');
started = false;}
});
}
},225);
|
74249687
Luigi Serra
Cross browser con...
|
176
177
178
179
180
|
}
});
});
};
|
a1a3bc73
Luigi Serra
graphs updates
|
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
var repositionWithinScreen = function(x, y, width, height) {
var newX = x
var newY = y;
if (newX < 0) {
newX = 4;
} else if (newX + width > window.innerWidth) {
newX -= newX + width - window.innerWidth;
}
if (newY < 0) {
newY = 4;
} else if (newY + height > window.innerHeight + $(window).scrollTop) {
newY -= newY + height - window.innerHeight;
}
return {x: newX, y: newY};
};
|
74249687
Luigi Serra
Cross browser con...
|
200
201
202
203
|
$(document).ready(function(){
$('.tooltipped').tooltip();
});
}( jQuery ));
|