jQuery tooltip

jQuery tooltip

Sorry for the lack of updates recently but I have been swept off my feet with work. A couple days ago I redesigned part of my companies website, Centation, whereby I needed to make a little jQuery tooltip to add a dynamic flair to the page. Well, I didn’t need to make one, but the lack of quality jQuery tooltip plugins forced me to. I thought I would give you the code of how I did this, and give you a couple demonstrations.

The HTML

Let’s start with some nice semantic markup.

  1. <div class="container">
  2.     <div class="hover">
  3.         <p>Hello. I'm a tooltip.</p>
  4.     </div>
  5.     <div>
  6.         <a href="page.html"><img src="image.jpg" alt="Image alt" /></a>
  7.     </div>
  8. </div>

We have a container which will contain the tooltip and the text/image/anything which you want a tooltip to appear when hovered over. I have put the tooltip in a div with the class hover. Nice simple stuff.

The jQuery

Obviously wee need the jQuery core, but we also need this little bit of jQuery:

  1. $(document).ready(function() {
  2.     $(".container").hover(function() {
  3.         if ($(".hover", this).css("display") == "block") { return false; }
  4.         os = $(this).offset();
  5.         $(".hover", this).css({top: os.top+"px", left: os.left+60+"px"}).animate({top: os.top-60+"px", opacity: "show"}, 750);
  6.     },
  7.     function() {
  8.         os = $(this).offset();
  9.         $(".hover", this).animate({top: os.top+"px", opacity: "hide"}, 500);
  10.     });
  11. });

It might look a bit of a mess, and I would agree with you there, but there is logic in this. The only bits of this code you need to understand and change are the CSS positioning and the animates.

The CSS intially places the hover in the correct position using the position of the hovered over element. You can change this to make the hover appear a little bit high, lower, to the left, to the right, or any combination of those.

The animation says where the hover element will move to. Again we use the offset of the hovered over element to base our position. As with the CSS positioning we can set it to go to the top, right, bottom or left.

What does it look like in action?

It looks great. I have created a demo page with two examples, and you can se the effect of this on my companies, Centation, portfolio.


Related articles

Top | Post a comment | Permalink

User submitted comments

1 Smithy wrote

21st June, 2008


Very nice. I espcially like the check to make sure that the tooltip isn’t already shown before animating it, most tips don’t do this which can result in multiple tooltips popping up or a continuous stream one after the other.

2 Christopher Hill wrote

21st June, 2008


Yes, I noticed that too Smithy.

My favourite part of this tooltip is that I can position the tooltip literally anywhere on the page—I don’t have to make it go over the hovered element if I don’t want to. Also, the fact that that the tooltip is positioned inside the container element, you can actually hover over the tooltip and it will not go away :-)

3 Patrick wrote

21st June, 2008


Wow, these tooltips look absolutely fantastic.

Thanks for the tutorial

4 George Swan wrote

22nd June, 2008


Sweet. Im definitly using these on my next project.

Just need to work out where I need them :P

5 hardik akbari wrote

27th June, 2008


thanx.. its really indigenius solution…


Penny for your thoughts

Comment posting guide

Your real name will be displayed as the author of the post. Real names are preferred, both first and second names, but nicknames/alter-egos are permitted. Any comments with the author name whose primary aim is to promote their website, and/or company will be removed.

If you supply a website URI then your real name will be clickable to that site. Only one sub-directory is permitted; e.g. http://www.example.org/directory.

Accepted comment input

  • <strong>…</strong>
  • <em>…</em>
  • <blockquote>…</blockquote>
  • <tt>…</tt>
  • <a href="…">…</a>
  • <code>…</code>




Recent articles

Other topics I ramble on about