-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtooltips.js
More file actions
67 lines (61 loc) · 2.04 KB
/
Copy pathtooltips.js
File metadata and controls
67 lines (61 loc) · 2.04 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
// Custom HTML tooltip logic with pointer interaction
document.addEventListener('DOMContentLoaded', () => {
const tooltip = document.getElementById('tooltip-box');
let hideTimeout = null;
function showTooltip(target) {
let contentHtml = '';
// Prefer data-tooltip-id referencing a reusable tooltip content
const tooltipId = target.getAttribute('data-tooltip-id');
if (tooltipId) {
const contentElem = document.getElementById(tooltipId);
if (contentElem) {
contentHtml = contentElem.innerHTML;
}
} else {
// Fallback: use inline .tooltip-content
const inlineContent = target.querySelector('.tooltip-content');
if (inlineContent) {
contentHtml = inlineContent.innerHTML;
}
}
tooltip.innerHTML = contentHtml;
tooltip.style.display = 'block';
const rect = target.getBoundingClientRect();
const tooltipRect = tooltip.getBoundingClientRect();
let left = rect.left + window.scrollX;
let top;
// Check if tooltip would overflow bottom of viewport
if (rect.bottom + 5 + tooltipRect.height > window.innerHeight) {
// Place above
top = rect.top + window.scrollY - tooltipRect.height - 5;
} else {
// Place below
top = rect.bottom + window.scrollY + 5;
}
tooltip.style.left = left + 'px';
tooltip.style.top = top + 'px';
}
function hideTooltip() {
tooltip.style.display = 'none';
}
document.body.addEventListener('mouseover', function(e) {
const target = e.target.closest('.tooltip');
if (target) {
clearTimeout(hideTimeout);
showTooltip(target);
}
});
document.body.addEventListener('mouseout', function(e) {
const target = e.target.closest('.tooltip');
if (target) {
// Delay hiding to allow pointer to move into tooltip
hideTimeout = setTimeout(hideTooltip, 200);
}
});
tooltip.addEventListener('mouseover', function() {
clearTimeout(hideTimeout);
});
tooltip.addEventListener('mouseout', function() {
hideTimeout = setTimeout(hideTooltip, 200);
});
});