I’ve got a custom tooltip being pulled by Ajax, when you rollover a link, great…e.g.
<script type="text/javascript">
$(document).ready(function(){
$('.tippytrip').hover(function(){
var tooltipId = this.hash;
$('#tooltip-container').empty().load('tooltips.html ' + tooltipId).show();
}, function(){
$('#tooltip-container').hide();
});});
</script>
So this shows the div… “tooltip-container”
But I think i’ll need some further jQuery assistance in actually positioning the tooltip next to each element. As CSS isn’t doing it…as it would dynamically need to be for each tooltip link hovered over.
So basically html is:
<a href="#tip1" class="tippytrip">Show Tip 1</a><br />
<a href="#tip2" class="tippytrip">Show Tip 2</a><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<a href="#tip1" class="tippytrip">Show Tip 1 - again</a>
<div id="tooltip-container"></div>
And then within tooltips.html I have:
<div id="tip1">
<h1>Hello there!</h1>
</div>
<div id="tip2">
<h1>Mr Tip 2</h1>
</div>
.offset()method to get the left and top positions of the.tippytriplink relative to the entire page.#tooltip-containercompletely outside of any other element (other than thebody, of course), and position it absolutely, using the position data from #1 to position it correctly.An example: http://jsfiddle.net/5LSxG/
And here’s the JS code. Pretty simple:
I recommend going with the
.offset()+ “outside of any other element” combination because the alternative would be:.position(), which gives the elements offset from its nearest relatively positioned parent.The problem with this is that simply re-positioning the tooltip wouldn’t be enough. You’d also have to insert it into a different part of the DOM each time the relative parent of the link in question changes.