I've researched this quite well but 开发者_运维技巧haven't managed to find anything, it would of probably been quicker to write it myself! I have content contained within 'li' tags, within the the li are DIVs which are hidden from the user. I'm basically looking for something that when you mouseover the specific li, the div contained within it pops up and hovers in relation to the mouse all the time the cursor is over the li. does anyone know something that will do exactly that?
You can accomplish this with just a bit of jQuery and CSS:
HTML:
<ul>
<li>
<div>Content goes here</div>
<div class="tooltip">Tooltip!</div>
</li>
<li>
<div>More content!</div>
<div class="tooltip">Another tooltip!</div>
</li>
</ul>
CSS:
.tooltip {
position:absolute;
display:none;
z-index:1000;
background-color:black;
color:white;
border: 1px solid black;
}
JavaScript:
$("li").bind("mousemove", function(event) {
$(this).find("div.tooltip").css({
top: event.pageY + 5 + "px",
left: event.pageX + 5 + "px"
}).show();
}).bind("mouseout", function() {
$("div.tooltip").hide();
});
Here's what's happening:
- An event handler for the
mousemove
event is attached for every list item. - Inside this listener, the
find
method is used to find thediv
with classtooltip
that lives under theli
that the event occurred on. - The
top
andleft
CSS properties are set according to the location the event occurred on (the current location of the mouse) and thediv
is shown. - Another event handler for the
mouseout
event is attached that hides any tooltips that are showing.
You can tweak the selectors to reflect your exact HTML structure. Here's a working example: http://jsfiddle.net/andrewwhitaker/tSHZS/
Will this work for you?
http://flowplayer.org/tools/tooltip/index.html
I'm not sure if it specifically handles your scenario, where you want to show the contents of the div on mouseover of the li, but I know it can display HTML content.
EDIT
Understood wrong your question. See the source to this page:
http://www.lbstone.com/reference/jquery/follow_mouse.html
Here you can find the code that does what you want
精彩评论