-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtooltip.js
More file actions
131 lines (122 loc) · 3.05 KB
/
tooltip.js
File metadata and controls
131 lines (122 loc) · 3.05 KB
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
!function ($) {
var Tooltip = function (e) {};
Tooltip.prototype = {
constructor: Tooltip
, posClass: 'top'
, enter: function (e) {
this.$e = e || window.event;
this.$target = e.target || e.srcElement;
return this.getPos();
}
, getPos: function (e) {
var tar
, tarBox
, width
, height;
e = this.$e;
tar = this.$target;
tarBox = tar.getBoundingClientRect();
this.tarTop = tarBox.top;
this.tarBottom = tarBox.bottom;
this.tarWidth = tarBox.width;
this.tarHeight = tarBox.height;
return this.tip();
}
, tip: function () {
var template
, arrow
, inner;
template = document.createElement('div');
arrow = document.createElement('div');
inner = document.createElement('div');
template.className = "tooltip";
arrow.className = "tooltip-arrow";
inner.className= "tooltip-inner";
template.appendChild(arrow);
template.appendChild(inner);
this.$tip = template;
return this.setPos();
}
, setPos: function () {
var toTop
, toLeft
, tip
, tipBox
, twidth
, theight
, left
, top
, tar
, pos;
tar = $(this.$target);
toTop = tar.position().top;//position()Get the current coordinates of the first element in
//the set of matched elements, relative to the offset parent.
toLeft = tar.position().left;
this.setContent();
if(this.$target.parentNode.getElementsByClassName('tooltip').length < 1){
this.$target.parentNode.appendChild(this.$tip);
tip = document.getElementsByClassName('tooltip')[0];
this.tipBox = tip.getBoundingClientRect();
this.tipWidth = this.tipBox.width;
this.tipHeight = this.tipBox.height;
pos = this.posClass;
if(this.tarTop >= this.tipHeight) {
pos = "top";
}
else {
pos = "bottom";
}
tip.className = 'tooltip ' + pos;
switch(pos) {
case "top":
top = toTop - this.tipHeight -2;
left = toLeft + this.tarWidth/2 - this.tipWidth/2;
break;
case "bottom":
top = toTop + this.tarHeight + 2;
left = toLeft + this.tarWidth/2 - this.tipWidth/2;
break;
}
$('.tooltip')[0].style.top = top + "px";
$('.tooltip')[0].style.left = left + "px";
}
$('.tooltip .tooltip-arrow').css('marginLeft',(this.tipWidth - 12)/2);
return this.show();
}
, setContent: function () {
var dataset
, cont
, insCont;
dataset = this.$target.dataset;
cont = dataset.cont;
insCont = document.createTextNode(cont);
this.$tip.childNodes[1].appendChild(insCont);
}
, show: function (e) {
$('.tooltip').fadeTo('slow',0.8);
}
, leave: function (e) {
var tip;
e = e || window.event;
tip = this.$tip;
return function(){
if(tip && tip.parentNode){
tip.parentNode.removeChild(tip);
}
}();
/* return $('.tooltip').fadeOut('fast',function(){
if(tip && tip.parentNode){
tip.parentNode.removeChild(tip);
}
});
*/
}
}
var tooltip = new Tooltip();
$('.tip a').mouseenter(function (e) {
tooltip.enter(e);
});
$('.tip a').mouseleave(function (e) {
tooltip.leave();
});
}(window.jQuery);