Update: we have released a new version of smart hover box!
Update: thanks to some feedback, found and fixed an IE bug that threw an error the first time you hovered over an element.
Smart Hover Box is a simple Mootools 1.2 function that spun off of mootime, my mootools 1.2 javascript timepicker. I liked the smart positioning, so decided to take the idea a bit further. With the Smart Hover Box you can add a hover html elements to any other element by adding a single id.
Imagine your web design has a single set of instructions that you want to hover:
That is all you have to do. Just add the id “id_suffix” to the element you want to hover, where “id” is the id of the control element. You can even define your own suffix to match your css style.
Check out a live demo of the Smart Hover Box v0.1 here.
Install mootools 1.2, then copy and paste the code before the </head> tag in your html file, or save it as a .js and pull it in.
To add hover boxes to your website, all you have to do is call the Smart Hover Box function, and define the following parameters: delay, x offset, y offset, the smart box suffix, the close button class name.
Here is the Smart Hover Box javascript:
var smartHoverBox = function(boxTimer, xOffset, yOffset, smartBoxSuffix, smartBoxClose) { var smartBoxes = $(document.body).getElements('[id$=' + smartBoxSuffix + ']'); var closeElem = $(document.body).getElements('.' + smartBoxClose); var closeBoxes = function() { smartBoxes.setStyle('display', 'none'); }; closeElem.addEvent('click', function(){ closeBoxes() }).setStyle('cursor', 'pointer'); var closeBoxesTimer = 0; smartBoxes.each(function(item){ var currentBox = item.getProperty('id'); currentBox = currentBox.replace('' + smartBoxSuffix + '', ''); $(currentBox).addEvent('mouseleave', function(){ closeBoxesTimer = closeBoxes.delay(boxTimer); }); item.addEvent('mouseleave', function(){ closeBoxesTimer = closeBoxes.delay(boxTimer); }); $(currentBox).addEvent('mouseenter', function(){ if($defined(closeBoxesTimer)) $clear(closeBoxesTimer); }); item.addEvent('mouseenter', function(){ if($defined(closeBoxesTimer)) $clear(closeBoxesTimer); }); item.setStyle('margin', '0'); $(currentBox).addEvent('mouseenter', function(){ smartBoxes.setStyle('display', 'none'); item.setStyles({ display: 'block', position: 'absolute' }).setStyle('z-index', '1000000'); //coordinates and size vars and math var windowSize = $(window).getSize(); var windowScroll = $(window).getScroll(); var halfWindowY = windowSize.y / 2; var halfWindowX = windowSize.x / 2; var boxSize = item.getSize(); var inputPOS = $(currentBox).getCoordinates(); var inputCOOR = $(currentBox).getPosition(); var inputSize = $(currentBox).getSize(); var inputBottomPOS = inputPOS.top + inputSize.y; var inputBottomPOSAdjust = inputBottomPOS - windowScroll.y var inputLeftPOS = inputPOS.left + xOffset; var inputRightPOS = inputPOS.right; var leftOffset = inputCOOR.x + xOffset; if(halfWindowY < inputBottomPOSAdjust) { item.setStyle('top', inputPOS.top - boxSize.y - yOffset); if (inputLeftPOS < halfWindowX) { item.setStyle('left', leftOffset); } else { item.setStyle('left', (inputPOS.right - boxSize.x) - xOffset); }; } else { item.setStyle('top', inputBottomPOS + yOffset); if (inputLeftPOS < halfWindowX) { item.setStyle('left', leftOffset); } else { item.setStyle('left', (inputPOS.right - boxSize.x) - xOffset); }; }; }).setStyle('cursor', 'pointer'); }); };
To add the function to your web page:
window.addEvent('domready', function() { smartHoverBox( 1000, //delay before vanishing 30, //x offset 0, //y offset '_smarthbox', //smart hover box suffix 'smarthbox_close' //hover box close class ); });
<span id="instructions">instructions</span> <div id="instructions_smarthbox"> <ul> <li>Step one</li> <li>Step two</li> <li>Step three</li> </ul> </div>
*Including the mootools 1.2 core, the ‘Smart Hover Box’ javascript file, and a simple html file.
You may have to set body {margin:0} for the offset to register correctly in IE (a bug in IE’s position reporting).
If you have any problem with flickering boxes on reload, try setting the boxes to display: none in the css.
Currently, only a single instance of the function is supported, though you may have as many hover boxes with the same parameters as you like.
You can use this however you like, and please drop me a comment letting me know if you use it in your web development project and what you think. I also welcome any suggestions on how to make it easier to use, as well as feature requests.
Thanks and I hope you find it useful.

This work is dedicated to the Public Domain.