-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpletooltip.js
78 lines (63 loc) · 2.06 KB
/
simpletooltip.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
/*
|--------------------------------------------------------------------------
| Simple Tooltip
|--------------------------------------------------------------------------
|
| author: @gnzandrs
| license: GNU
| description: this code works with a javascript object wich provides to your
| html control a div container that allow show a tooltip.
|
*/
var simpleTooltip = function (elementId, tooltip) {
this.elementId = elementId;
this.tooltip = tooltip;
if (typeof this.elementId != 'string')
{
console.log('The element ID have to be a string in the object initiation.');
}
if (typeof this.tooltip != 'string')
{
console.log('The TOOLTIP have to be a string in the object initiation.');
}
this.createElementListeners = function() {
var element = document.getElementById(elementId);
var ttElement = document.getElementById('tt'+elementId);
element.onmouseover =
function() {
ttElement.className = '';
ttElement.className = 'tooltipShow';
};
element.onmouseleave =
function() {
ttElement.className = '';
ttElement.className = 'tooltipHide';
};
}
this.createElementTooltip = function() {
var toolTipDiv = document.createElement('div');
toolTipDiv.className = 'tooltipHide';
toolTipDiv.id = 'tt' + elementId;
var content = document.createTextNode(tooltip);
toolTipDiv.appendChild(content);
var element = document.getElementById(elementId);
element.parentNode.appendChild(toolTipDiv);
toolTipDiv.style.position = 'absolute';
toolTipDiv.style.top = '-10px';
toolTipDiv.style.left = '170px';
}
this.createElementParentDiv = function() {
var newParentDiv = document.createElement('div');
newParentDiv.style.position = 'relative';
var element = document.getElementById(elementId);
var elementParent = document.getElementById(elementId).parentNode;
newParentDiv.appendChild(element);
elementParent.appendChild(newParentDiv);
}
this.init = function() {
this.createElementParentDiv();
this.createElementTooltip();
this.createElementListeners();
}
this.init();
};