tooltip

In this example below you will see how to do a tooltip with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by ig90, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright ig90 ©
  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>tooltip</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div class="main"> 

<div id="test">show tooltip</div>
<div id="tmp">x position:</div>
<div id="tooltip"> Tooltip </div>

  </div>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/ig90/tooltip-rJNPwr */
#tooltip{

	display: none;
	background-color: teal;
	position: absolute;
	width: 120px;
	height: 40px;
  padding-top:20px;
  
}

.main{
  text-align: center;
}


#test{
  width: 30%;
  display: inline-block;
  background: lightgrey;
  height: 30px;
  line-height: 30px;
}

/*Downloaded from https://www.codeseek.co/ig90/tooltip-rJNPwr */
function showTooltip(event){

	var e = event || window.event;
	var tmp = document.getElementById("tmp");

	tmp.innerHTML ="x position: "+ e.clientX + " px";
  
	var tooltip = document.getElementById("tooltip");
	tooltip.style.display = "block";

	tooltip.style.left = e.clientX + 15 + "px";
	tooltip.style.top = e.clientY + 15 + "px";

}

window.onload = function(){

	var test = document.getElementById("test");

//	test.onmousemove = showTooltip;
	test.onmousemove = function(event){
  
  	showTooltip(event);
  };
  
  test.onmouseout = function(){
    //var tooltip = document.getElementById("tooltip");
  	tooltip.style.display = "none";
    tmp.innerHTML = "x position: ";
  };
  
};

Comments