Javascript Image Tooltip

Of course you do! That's why you use the iWebs right?

For something we did at work the other day, I needed to create a javascript lib that would let me hover my mouse over a thumbnail, and float the full sized image above the thumbnail, and track the cursor.

Here's the guts of it:
Javascript
  1. var tip;
  2. var tipBoxName;
  3.  
  4. /**
  5.  * The guts of the tooltip
  6.  * @param img The Image URL to show
  7.  * @param tipBox The name of the <span> element to use (for multiple tooltips)
  8.  * @param that That is "this" passed from the calling icon
  9.  */
  10. function toolTip(img,tipBox,that)
  11. {
  12.     tipBoxName = tipBox;
  13.     tip = that;
  14.     tip.onmousemove=updatePos;
  15.     document.getElementById(tipBoxName).innerHTML='<img src="' + img + '" alt=""/>';
  16.     document.getElementById(tipBoxName).style.display="block";
  17.     window.onscroll=updatePos;
  18. }
  19.  
  20. /**
  21.  * Track the movements of the cursor
  22.  */
  23. function updatePos()
  24. {
  25.     var ev = arguments[0] ? arguments[0] : event;
  26.     var x = ev.clientX;
  27.     var y = ev.clientY;
  28.     diffX=-320;
  29.     diffY=-100;
  30.     document.getElementById(tipBoxName).style.top  = y-2+diffY+document.body.scrollTop + "px";
  31.     document.getElementById(tipBoxName).style.left = x-2+diffX+document.body.scrollLeft + "px";
  32.     tip.onmouseout=hideToolTip;
  33. }
  34.  
  35. /**
  36.  * This method brushes you teeth for you...
  37.  * Actally, it just hides the tooltip when you
  38.  * fall off the icon you're hovering over
  39.  */
  40. function hideToolTip()
  41. {
  42.     document.getElementById(tipBoxName).style.display="none";
  43. }


Here's the accompanying CSS for the tooltip:
CSS
  1. .toolTipBox
  2. {
  3.     display: none;
  4.     padding: 5;
  5.     border: black solid 1px;
  6.     position: absolute;
  7.     background-color: #FFFFFF;
  8. }


Here's an HTML Example:
HTML
  1. <span id="toolTipBox_1" class="toolTipBox"></span>
  2. <img src="images/picture_icon.png" alt="Image Preview" border="0" onmouseover="toolTip('images/bigImage.png', 'toolTipBox_1', this);" />


What's Going On?


So we have our thumbnail image, picture_icon.png. We have a span element with a unique ID, and it uses the CSS class toolTipBox. The span is hidden by default, and is only shown when we mouseover our thumbnail. As you can see, the onmouseover event for the thumbnail calls our toolTip method, passes in the image we want to display in the tooltip, the span we want to show it in, and this so we know what the calling element is in order to capture the mouse position on it.

So there you have it. Want to see an example? Hover over any of the Recent or Archive blog post listings on the left. See?

You could include text with the image, or not use an image at all! You could put text in there, or an iframe and call in a website... The world is your oyster. The hard part is the c=attribute]updatePos()[/c] method. You'll notice that diffX = -320, and diffY = -100. That should position the tooltip directly above the cursor, and about 20px above. Why those values? I have no idea.... That's what worked for me. Obviously, you can tweek those values to position your tooltip wherever you like. It's your life!