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.
<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" 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>
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.
#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;
}
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.
jQuery(document).ready(function(){
$("#iconbar li").hover(
function(){
var iconName = $(this).find("img").attr("src");
var origen = iconName.split(".")[0];
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?).
$(this).find("img").attr({src: "" + origen + "o.gif"});
$(this).find("span").animate({opacity: "show", top: "-60"}, "normal");
},
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.
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");
});
});
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.
<li><a href="#"> <img src="sel.gif" alt="" /> <span>Choose your options!<img src="sel.gif" alt="" /></span> </a></li>
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.
If you liked this you might want to buy me a coffee :)
Posted on Wednesday, November 19th, 2008 in .
RSS 2.0 feed for comments.



![[del.icio.us]](http://www.ilovecolors.com.ar/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.ilovecolors.com.ar/wp-content/plugins/bookmarkify/digg.png)
![[Facebook]](http://www.ilovecolors.com.ar/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://www.ilovecolors.com.ar/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://www.ilovecolors.com.ar/wp-content/plugins/bookmarkify/linkedin.png)
![[MySpace]](http://www.ilovecolors.com.ar/wp-content/plugins/bookmarkify/myspace.png)
![[Reddit]](http://www.ilovecolors.com.ar/wp-content/plugins/bookmarkify/reddit.png)
![[StumbleUpon]](http://www.ilovecolors.com.ar/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Technorati]](http://www.ilovecolors.com.ar/wp-content/plugins/bookmarkify/technorati.png)
![[Twitter]](http://www.ilovecolors.com.ar/wp-content/plugins/bookmarkify/twitter.png)
![[Yahoo!]](http://www.ilovecolors.com.ar/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.ilovecolors.com.ar/wp-content/plugins/bookmarkify/email.png)




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
November 22nd, 2008 at 10:41 am
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.
November 22nd, 2008 at 2:17 pm
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.
December 4th, 2008 at 3:24 am
Yes r.tatem, tt’s possible. I’ll write an article soon explaining how to fade from on image to the other.
December 4th, 2008 at 3:42 am
Can you pakcage that in a sample project to download?
December 15th, 2008 at 2:28 pm
Very impressive stuff … amongst the top jquery effects from an aesthetic perspective
February 8th, 2009 at 3:06 am
*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!
February 19th, 2009 at 4:56 am
LOL I’m so happy you endless search has come to an end
Enjoy it.
February 19th, 2009 at 12:58 pm
I find solution here.
February 24th, 2009 at 6:34 am
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?
March 11th, 2009 at 6:35 am
jQuery will certainly work on your ASP pages since it operates on the DOM after it is ready. Microsoft didn’t bought jQuery. jQuery will be included in Visual Studio. you can see the news from last year on the jQuery blog:
http://blog.jquery.com/2008/09/28/jquery-microsoft-nokia/
March 11th, 2009 at 9:40 pm
[...] DIRECT LINK » [...]
July 5th, 2009 at 1:42 pm
This is an amazing tutorial, very nicely written and easy to follow. I have checked out the blog too… an amazing design, Nicely done!
July 7th, 2009 at 4:21 am
[...] Tutorial Demo [...]
July 7th, 2009 at 1:19 pm
Thanks Jake, I’m glad you’ve find it useful and thanks for your kind words about the site =)
July 7th, 2009 at 1:32 pm
[...] Tutorial Demo [...]
July 8th, 2009 at 10:57 am
Super!!!
July 13th, 2009 at 10:34 am
Very nice… Tks !!!
July 18th, 2009 at 3:53 am
[...] Rollovers and tooltips with jQuery [...]
July 21st, 2009 at 1:07 am
[...] http://www.ilovecolors.com.ar/rollovers-tooltips-jquery/ [...]
August 17th, 2009 at 4:33 pm
[...] Rollovers and tooltips with jQuery : View Demo [...]
August 17th, 2009 at 7:31 pm
[...] Tutorial Demo [...]
August 21st, 2009 at 4:56 pm
[...] Visit Tutorial – Demo [...]
August 25th, 2009 at 5:43 pm
[...] Rollovers and tooltips with jQuery [...]
September 1st, 2009 at 6:58 pm
[...] Rollovers and tooltips with jQuery [...]
September 3rd, 2009 at 4:19 am
Thanks a lot buddy.. really useful
September 4th, 2009 at 1:49 pm
September 4th, 2009 at 9:44 pm
[...] Rollovers and tooltips with jQuery | jQuery | ilovecolors Rollovers y tooltips con jQuery (tags: jquery rollover tooltip javascript ajax) [...]
October 5th, 2009 at 4:04 pm
[...] Rollovers and tooltips with jQuery [...]
October 9th, 2009 at 9:07 am
really useful
October 15th, 2009 at 6:00 pm
[...] a tooltip. I thought about taking advantage of html tags attributes manipulation through jQuery. Web Site Demo Download AKPC_IDS += "699,";Popularity: unranked [?] Share and [...]
October 17th, 2009 at 6:04 am
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.
November 3rd, 2009 at 7:51 pm
your site is beautiful. love the graphics
November 16th, 2009 at 12:05 am
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.
November 18th, 2009 at 2:09 am
Конечно же присоединяюсь к вышесказанному!
January 5th, 2010 at 8:35 am
[...] jquery-rollover-tooltip [...]
January 9th, 2010 at 10:35 pm
[...] The most popular are the articles about some interface elements tutorials using the jQuery library, like rotating tabs, sliding menus or rollovers and tooltips. [...]
January 28th, 2010 at 2:28 pm
Hello everybody, we agree this is a good stuf but can anyone tell me how it work on IE browser? thank
February 6th, 2010 at 11:56 am
It works on IE7
It should work on all browsers since it’s a fairly simple technique
February 6th, 2010 at 1:38 pm
Its simple, but works great…
Thanks for sharing this…
March 10th, 2010 at 5:03 am