-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtooltip.js
38 lines (34 loc) · 881 Bytes
/
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
// Tooltip template
const tooltipHTML = data => `
<p class="tooltip__title">${data.title}</p>
<ul class="tooltip__list">
${data.items.map(el => `
<li class="tooltip__list-item">
<p class="tooltip__value" style="color: ${el.color}">${el.value}</p>
<p class="tooltip__name" style="color: ${el.color}">${el.name}</p>
</li>
`).join('')}
</ul>
`;
// Generate tooltip
const tooltip = el => {
// Clear tooltip contents
const clear = () => el.innerHTML = '';
return {
// Show tooltip method
show({ left, top }, data) {
const { width, height } = el.getBoundingClientRect();
clear();
el.style.cssText = `
display: block;
top: ${top - height * 1.5}px;
left: ${left - width / 6}px;
`;
el.insertAdjacentHTML('afterbegin', tooltipHTML(data));
},
// Hide tooltip method
hide() {
el.style.cssText = 'display: none';
}
};
};