Simple Tooltip (JS)

HTML
<br/> <span class="tooltip" title="I am a a span." >Hover over me</span> <br/><br/> <div class="tooltip" title="I am a div." >Hover over me</div>
CSS
#ttipWrap{ display:none; position:fixed; z-index:100000; background-color:#444; text-align:center; padding:10px; box-sizing:border-box; color:#eeeeee; border-radius:2px; box-shadow:0 0 2px #555; word-wrap:break-word; width:auto; max-width:300px; min-width:100px; }
JAVASCRIPT
(function tooltip(classname){ var ttips = document.getElementsByClassName(classname); if(!document.getElementById("ttipWrap")){ var ttipWrap = document.createElement("DIV"); ttipWrap.id = "ttipWrap"; document.body.appendChild(ttipWrap); } for (var i = ttips.length - 1; i >= 0; i--) { var existsAlready = ttips[i].getAttribute("data-ttip"); if( !existsAlready || existsAlready.length <= 0 ){ //added in v1.1 var data = ttips[i].getAttribute("title"); ttips[i].setAttribute("title",""); ttips[i].setAttribute("data-ttip",data); } ttips[i].onmouseover = ttipShow; ttips[i].onmouseout = ttipHide; } function ttipShow(e){ var ttipWrap = document.getElementById("ttipWrap"), ex = e.clientX, ey = e.clientY; ttipWrap.innerHTML = this.getAttribute("data-ttip"); ttipWrap.style.top = ey + 10 + "px"; if( (ttipWrap.offsetWidth + ex) < window.innerWidth ){ ttipWrap.style.left = ex + 10 +"px"; }else{ ttipWrap.style.left = window.innerWidth - ttipWrap.offsetWidth +"px"; } ttipWrap.style.display = "block"; this.onmousemove = ttipShow; } function ttipHide(){ document.getElementById("ttipWrap").style.display = "none"; } })("tooltip");
Expand for more options Login