Created
April 6, 2017 01:28
-
-
Save jonathanborges/5d82e34106e5a1d1b26518c92917bf31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class Bs { | |
constructor(selector) { | |
this.selector = selector; | |
} | |
tooltip() { | |
let uid = 0; | |
document.querySelectorAll(this.selector).forEach(el => { | |
el.addEventListener('mouseover', ev => { | |
let tooltipId = 'tooltip'+(++uid); | |
ev.target.setAttribute('aria-describedby', tooltipId); | |
let position = ev.target.getBoundingClientRect(); | |
let template = ` | |
<div class="tooltip tooltip-top" role="tooltip" id="${tooltipId}"> | |
<div class="tooltip-inner">${ev.target.title}</div> | |
</div> | |
`; | |
document.querySelector('body').insertAdjacentHTML('afterbegin', template); | |
let elSize = position.left-position.right; | |
let tooltipSelected = document.getElementById(tooltipId); | |
tooltipSelected.style.cssText = ` | |
position: absolute; | |
top: ${(position.top-40)+'px'}; | |
left: ${(position.left+elSize/2)+'px'}; | |
`; | |
this.fadeIn(tooltipSelected); | |
}); | |
el.addEventListener('mouseout', ev => { | |
let nodeEl = document.getElementById(ev.target.getAttribute('aria-describedby')); | |
this.fadeOut(nodeEl); | |
}); | |
}); | |
} | |
fadeOut(el) { | |
var timerOut = setInterval(function() { | |
el.style.opacity -= 0.02; | |
}, 10); | |
if (el.style.opacity == 0) { | |
clearInterval(timerOut); | |
} | |
} | |
fadeIn(el) { | |
var timerIn = setInterval(function() { | |
el.style.opacity += 0.02; | |
}, 10); | |
if (el.style.opacity == 1) { | |
clearInterval(timerIn); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment