I have some toy code for a tooltip. It works okay, except that when you mouse into the tooltip it gets hidden with this logic.
I’m banging my head trying to figure out a clean way to get the tooltip to stay visible when you mouse into it, and disappear when you mouse out of both areas. Anyone have a suggestion?
HTML
<div id="container">
<div id="div1"></div>
<div id="div2"></div>
</div>
Javascript
var div1 = $("#div1");
var div2 = $("#div2").hide();
var hoverTimer;
div1.mouseenter(function(e) {
var x = e.pageX;
var y = e.pageY;
div1.mousemove(function(e) {
x = e.pageX;
y = e.pageY;
});
hoverTimer = window.setTimeout(function() {
div2.css("left", x);
div2.css("top", y);
div2.show();
}, 400);
});
div1.mouseleave(function(e) {
window.clearTimeout(hoverTimer);
div2.hide();
});
CSS
#container {
position: relative;
}
#div1 {
float: left;
clear: none;
width: 200px;
height: 200px;
background-color: green;
}
#div2 {
position: absolute;
width: 100px;
height: 100px;
background-color: blue;
}
You create the
mousemoveevent, but aside from setting two variables you never actually move the tooltip. Try this instead. Note I added a 16-pixel “margin” on the mouse position – this is to make it more inline with actual tooltips, and prevents the flickering effect.