This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtooltip.js
58 lines (58 loc) · 2.16 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Tooltip {
/**
* constructor
* @param {HTMLElement} element - The element to bind
* @param {string} title - The tooltip callback
* @param {string} selector - The tooltip selector, append to the id
*/
constructor(element, title, selector = null) {
// get the element
this.element = element;
// store the data-title or the title callback
this.title = 'function' === typeof title ? title() : this.element.dataset.title;
// if the title isn't set or empty, break
if (this.title.length === 0) {
return false;
}
// add onmouseenter and onmouseleave listerner
this.element.onmouseenter = this.show.bind(this);
this.element.onmouseleave = this.hide.bind(this);
// get the tooltip element
selector = ['tooltip', selector].filter(String).join('-');
this.target = document.getElementById(selector);
// if not, create it
if (this.target === null) {
this.target = document.createElement('div');
this.target.id = selector;
document.body.appendChild(this.target);
}
}
/**
* show the tooltip
*/
show() {
// set the title into the tooltip
this.target.innerHTML = this.title;
// get the element offset relative to the client window
const offset = this.element.getBoundingClientRect();
// check the top and left position
let top = offset.top < window.innerHeight / 2,
left = offset.left < window.innerWidth / 2;
// add some classes
this.target.classList.add(top ? 'top' : 'bottom');
this.target.classList.add(left ? 'left' : 'right');
// set positions
this.target.style.top = (top ? offset.bottom : offset.top - this.target.offsetHeight) + 'px';
this.target.style.left = (left ? offset.left : offset.right - this.target.offsetWidth) + 'px';
// display
this.target.style.opacity = 1;
}
/**
* hide the tooltip
*/
hide() {
// remove classes
this.target.removeAttribute('class');
this.target.removeAttribute('style');
}
}