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…

6 Michael Plant wrote

8th October, 2008


Great tooltips!

My only issue is with IE7 rendering. And this isn’t the first time I have seen JQuery’s issues with fades and cleartype text in IE7. I even have found some great articles on the issue. But I can’t understand why no one has presented a fix for this and applied it to the transitions in JQuery Core!?!

Matt Berseth has a great article detailing the issue, but how do you apply that to existing works? IE7, ClearType, DXImageTransforms and Fade Animation Fuzziness goes into how to fix it, for now I have opted to not use the fade effect until the JQuery community has repaired this.

I also noticed that the drop shadow also is kinda screwy until the transition ends? Any Ideas on how to fix this and is there a reason I am the only one who checks IE anymore?

With all due respect,

7 Michael Plant wrote

8th October, 2008


Great tooltips!

My only issue is with IE7 rendering. And this isn’t the first time I have seen JQuery’s issues with fades and cleartype text in IE7. I even have found some great articles on the issue. But I can’t understand why no one has presented a fix for this and applied it to the transitions in JQuery Core!?!

Matt Berseth has a great article detailing the issue, but how do you apply that to existing works? IE7, ClearType, DXImageTransforms and Fade Animation Fuzziness goes into how to fix it, for now I have opted to not use the fade effect until the JQuery community has repaired this.

I also noticed that the drop shadow also is kinda screwy until the transition ends? Any Ideas on how to fix this and is there a reason I am the only one who checks IE anymore?

With all due respect,

8 Jaroslaw Kosinski wrote

18th October, 2008


Nice tooltip Chris Looks great, respect for this Cheers

9 A P wrote

26th December, 2008


This is nice example

10 stephane pautrat wrote

3rd February, 2009


Great for your tooltip.

But one question, how on the same page can you have 2 hover-top ?

thanks

11 stephane pautrat wrote

4th February, 2009


Great for your tooltip.

But one question, how on the same page can you have 2 hover-top ?

thanks

12 Christopher Hill wrote

19th February, 2009


@stephane pautrat Because you create separate elements on the page for each popup - the jQuery references each one.


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>