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.
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 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.
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.
Posted on Wednesday, November 19th, 2008 in .
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
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.
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.
Yes r.tatem, tt’s possible. I’ll write an article soon explaining how to fade from on image to the other.
Can you pakcage that in a sample project to download?
Very impressive stuff … amongst the top jquery effects from an aesthetic perspective
*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!
LOL I’m so happy you endless search has come to an end
Enjoy it.
I find solution here.
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?
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/
[...] DIRECT LINK » [...]
This is an amazing tutorial, very nicely written and easy to follow. I have checked out the blog too… an amazing design, Nicely done!
[...] Tutorial Demo [...]
Thanks Jake, I’m glad you’ve find it useful and thanks for your kind words about the site =)
[...] Tutorial Demo [...]
Super!!!
Very nice… Tks !!!
[...] Rollovers and tooltips with jQuery [...]
[...] http://www.ilovecolors.com.ar/rollovers-tooltips-jquery/ [...]
[...] Rollovers and tooltips with jQuery : View Demo [...]
[...] Tutorial Demo [...]
[...] Visit Tutorial – Demo [...]
[...] Rollovers and tooltips with jQuery [...]
[...] Rollovers and tooltips with jQuery [...]
Thanks a lot buddy.. really useful
[...] Rollovers and tooltips with jQuery | jQuery | ilovecolors Rollovers y tooltips con jQuery (tags: jquery rollover tooltip javascript ajax) [...]
[...] Rollovers and tooltips with jQuery [...]
really useful
[...] 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 [...]
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.
your site is beautiful. love the graphics
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.
Конечно же присоединяюсь к вышесказанному!
[...] jquery-rollover-tooltip [...]
[...] The most popular are the articles about some interface elements tutorials using the jQuery library, like rotating tabs, sliding menus or rollovers and tooltips. [...]
Hello everybody, we agree this is a good stuf but can anyone tell me how it work on IE browser? thank
It works on IE7
It should work on all browsers since it’s a fairly simple technique
Its simple, but works great…
Thanks for sharing this…
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
Thankyou, this is really helpful information, much appreciated.
Thankyou!
It’s very used!!
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.
awesome
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!
i want jquery slider with pagination and also with fade effect. pls help
nice job!
awesome!
i have some problem with chrome..it’s only my problem? thanks!!!!
I found your post vry useful! I wont hesitate to recommend your contribution.
Cheers!
Ana