-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateJS-codeForTooltipBehavior.html
More file actions
75 lines (55 loc) · 1.72 KB
/
CreateJS-codeForTooltipBehavior.html
File metadata and controls
75 lines (55 loc) · 1.72 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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Tooltip</title>
<style>
body {
height: 2000px;
}
.tooltip {
position: fixed;
padding: 10px 20px;
border: 1px solid #b3c9ce;
border-radius: 4px;
text-align: center;
font: italic;
color: #333;
background: #fff;
box-shadow: 3px 3px 3px rgba(0, 0, 0, .3);
}
</style>
</head>
<body>
<p>lorem Ipsum</p><p>lorem Ipsum</p><p>lorem Ipsum</p><p>lorem Ipsum</p><p>lorem Ipsum</p><p>lorem Ipsum</p><p>lorem Ipsum</p><p>lorem Ipsum</p><p>lorem Ipsum</p><p>lorem Ipsum</p><p>lorem Ipsum</p><p>lorem Ipsum</p>
<button data-tooltip="the tooltip is longer than the element">Short button</button>
<button data-tooltip="HTML<br>tooltip">One more button</button>
<script>
let tooltipElem;
document.onmouseover = function(event) {
let target = event.target;
let tooltipHtml = target.dataset.tooltip;
if (!tooltipHtml) return;
tooltipElem = document.createElement('div');
tooltipElem.className = 'tooltip';
tooltipElem.innerHTML = tooltipHtml;
document.body.append(tooltipElem);
let coords = target.getBoundingClientRect();
let left = coords.left + (target.offsetWidth - tooltipElem.offsetWidth) / 2;
if (left < 0) left = 0;
let top = coords.top - tooltipElem.offsetHeight - 5;
if (top < 0) {
top = coords.top + target.offsetHeight + 5;
}
tooltipElem.style.left = left + 'px';
tooltipElem.style.top = top + 'px';
};
document.onmouseout = function(e) {
if (tooltipElem) {
tooltipElem.remove();
tooltipElem = null;
}
};
</script>
</body>
</html>