Rollovers and tooltips with jQuery

Yesterday, I needed a quick setup for doing some rollovers and tooltips. You know, the usual stuff, mouse over an icon changes image and shows a tooltip. I thought about taking advantage of html tags attributes manipulation through jQuery. If you want to jump in right now you can go to the

jQuery rollovers and tooltips example page

Now, if you’re still with me, let’s take a walk through the code. This tutorial assumes that you have a basic knowledge of html, css and jquery.

The markup

This is pretty straight, just an unordered list with three items and each item has a link.

[code language=”html”]
<ul id="iconbar">
<li><a href="#">
<img src="key.gif" alt="" />
<span>Change your password</span>
</a></li>
<li><a href="http://feeds.feedburner.com/ilovecolors&quot; target="_blank">
<img src="rss.gif" alt="" />
<span>Suscribe to our RSS!</span>
</a></li>
<li><a href="#">
<img src="sel.gif" alt="" />
<span>Choose your options!</span>
</a></li>
</ul>
[/code]

However, inside each link we have two important blocks: the icon image and a span. On the img tag, we’ll be changing its src attribute.  The span tag is what we will be using for the tooltip. Initially the img tag has a default src value and the span is hidden, when the time comes and the hover event is triggered, the img src attribute is redirected to a different location (another image) and the span is displayed showing the text you defined.

The styling

The style is a bit longer to read but we’re only styling two items: the li containter and the span block.

[code language=”css”]

#iconbar li {
float:left; position:relative; margin-right:20px;
}
#iconbar span {
position: absolute;
top: -50px;
left: -80px;
display: none;
background: url(ttbg.jpg) no-repeat;
width: 110px;
height: 35px;
text-align: center;
padding: 5px;
line-height:110%;
color:#000000;
}
[/code]

We’re defining an absolute position inside the relative position of our li element. Now we can assign some top and left values to position it. The values will be modified by our jQuery script later. The display is set to none, since we don’t want the span to be visible when we first open the page. The icons doesn’t require styling since we’re changing only the src attribute through jQuery.

The jQuery code

jQuery is truly amazing, with only a few lines of code we can have our icon rollover and tooltip ready. So, let’s get to it. When the DOM is ready, we’re launching a function based on the hover state of the li element within our iconbar. When it’s launched, it will get the src attribute of the image within li and it will store the name without the extension in a variable named origen.

[code language=”js”]
jQuery(document).ready(function(){
$("#iconbar li").hover(
function(){
var iconName = $(this).find("img").attr("src");
var origen = iconName.split(".")[0];
[/code]

Next, we assign a new path to the src attribute of img, appending “o” to the name, and the corresponding extension, in this case, “.gif”. The tooltip section begins animating its opacity to fade in and the top position (remember the values on the style sheet?).

[code language=”js”]
$(this).find("img").attr({src: "" + origen + "o.gif"});
$(this).find("span").animate({opacity: "show", top: "-60"}, "normal");
},
[/code]

Please, notice the comma separating the two functions. The hover event in jQuery takes two parameters: one for the mouse rollover state and one for the mouse rollout state. Now we get to the rollout state, this is what happens when the mouse rolls out of the li area. We take the image src attribute again and we split at the “o.”, the string we added on the hover event, and we redirect the src attribute to the original image path. The tooltip is animated towards a zero opacity, and we position it at the same top value that is defined in the stylesheet, in this case, 50. Notice that I haven’t added the “px” values. Don’t be as lazy as me and add them, but you can see that it still works without the “px” dimensions. We hide it fast to prevent any conflicts with any other thing. You know, when something can go wrong, it will, so don’t give it a chance.

[code language=”js”]
function(){
var iconName = $(this).find("img").attr("src");
var origen = iconName.split("o.")[0];
$(this).find("img").attr({src: "" + origen + ".gif"});
$(this).find("span").animate({opacity: "hide", top: "-50"}, "fast");
});
});
[/code]

Of course, some of you might be thinking that you can place anything inside that little cute span and indeed, it’s possible. For example, an icon.

[code language=”html”]
<li><a href="#">
<img src="sel.gif" alt="" />
<span>Choose your options!<img src="sel.gif" alt="" /></span>
</a></li>
[/code]

Next time we will be learning to do some sliding tooltips triggering the same hover event. Take for example an iconbar. When you hover the li items the li expands and the tooltip is revealed.

That’s all, so you can go ahead and make your own icon rollover with tooltip. Make sure you check the jQuery rollover and tooltip example page and the Free web development icons #4 SE from IcoJoy. Bye for now.

UPDATE: as the user faani requested, you can download the rollovers and tooltips with jquery project for close inspection.

66 thoughts on “Rollovers and tooltips with jQuery”

  1. jQuery is good for modern web sites… but some browser have problems with it… web developers need a lot of hours to fix every bugs and problems with old browsers (eg IE6)… but i like jQuery and every kind of high tech web develope metods

  2. Marko, I have to disagree. I’ve done jQuery implementations many times and they work smooth in browsers. I’ve had some annoyances with FF2, but targeting it and applying a different effect made the trick.
    I will post later how to target different browsers using jQuery.

  3. cool effects. i’m new to jquery so what i’d like to see if possible is to fade from one icon to the other on rollover as opposed to the classic image swap.

    but nice none the less!
    thx.

  4. Yes r.tatem, tt’s possible. I’ll write an article soon explaining how to fade from on image to the other.

  5. *collapses in a grateful heap on the floor*

    You would not BELIEVE the number of days’ work I’ve wasted searching for a decent tooltip, trying them out, eventually throwing each one away for various reasons and starting again…

    Your tooltip is perfect for the project I’m working on. Thank you so much for sharing it with us – you saved my ass!

  6. I am a asp.net guy, and have been using the AJAX toolkit that is provided by Microsoft.

    Can JQuery work with my asp.net web apps?

    Is JQuery “bought” by MS?

  7. This is an amazing tutorial, very nicely written and easy to follow. I have checked out the blog too… an amazing design, Nicely done!

  8. Thanks Jake, I’m glad you’ve find it useful and thanks for your kind words about the site =)

  9. In the latest version of Opera (10.01) there’s a problem – glittering icons made this code impossible to use! Opera is popular in Europe, so it’s pity to see, that so many JQuery script are not tested under this browser.

  10. How do you make the animate effects reverse immediately when your mouse leaves the link? Instead, mine will go through the whole animation if you hover just for a split second.

  11. Hello everybody, we agree this is a good stuf but can anyone tell me how it work on IE browser? thank

    1. It works on IE7 🙂 It should work on all browsers since it’s a fairly simple technique

  12. Hi… after mouse over the icon one time, if put a mouse up the icon… ooopps show de hide span… ¿why?
    sorry 4 my english

  13. Hi, Thank you for this nice tutorial.

    I want to modify the javascript code so that the tool tip always display at top but in center of these three images.

    For this I have set the left value but it isn’t working correctly.

    I want to ask what modifications will be required for this?

    Thanks,
    Dazy.

  14. After I roll out from the small icon anytime I roll over the area where the pop up was it shows up again. How do you avoid this? Other than that great job! I appreciate you posting this!

  15. nice job!
    awesome!
    i have some problem with chrome..it’s only my problem? thanks!!!!

  16. Anybody got the magic tip how to vertical align the tekst in the balloon?

    Cheers!

    RAvert

  17. I am having an issue with getting the speech bubbles to appear above my images. They just default to the top left.

    1. To the top left of the browser? make sure the container for the link and the tooltip has position:relative in its CSS.

  18. Nice script but there is a probelm. The tip pop up can be selected by rolling over the actual tips as well as the image. The impact is that this affects other areas of the website and rescricts the user from accessing any data which may be behind the tool tips. Ideally the tips should only show when the user hold their mouse over the icon.

    1. Thank you very much Jon for the input! I’ve fixed the code, feel free to check the example or download the zip file again.

  19. Hi Elio,
    that seems to have done the trick. Its a nice touch. I write my own CMS websites so tool tips and tool bars are always handy for new users when learning by doing. Nice script 🙂

Comments are closed.