Tooltips are not the most exciting thing about the web. I mean you hover your mouse over a link or image for a couple of seconds and you get some text describing it. With all the really cool web apps out there, tooltips have lost their touch on the web. But, with a little help from jQuery, we can take these boring tips and replace them with our own fancy ones. Since jQuery is quite an amazing library, we can do this in a snap.
Tooltips are not a new feature at all. Most browsers have tooltips built into them, which work off of the title attribute being supplied to HTML tags. Whatever text is in the title attribute is what is displayed in the Tooltip. This is a pretty basic feature, but why fight it? Why not use the title attribute for our tooltips as well? Well, we actually can.
But the browser will also read this attribute and we will end up with a nasty double tooltip, which is never a good sign. In order to use the title attribute, we are going to have to be tricky. To start, let's go over the basics.
For the HMTL side of things, we are keeping it simple in this tutorial. All we need to get the point across are two links, with different "titles":
<a href="http://www.switchonthecode.com" title="This is another link">This is another link</a>
Now, in order for our custom tooltip to work properly, we need our div to be in the body of our page. To position it correctly, it cannot be inside any other tag. This is why we are going to use jQuery to create the div:
$("body").append("<div id='ToolTipDiv'></div>");
});
This will put the div inside the body, outside of any other tag. However, in order for the tooltip to start out hidden, and for some extra cool factor, we need to add a style to the div:
Position: absolute;
top: 100px;
left: 100px;
height: 20px;
border: 2px solid #FF0000;
background-color: #FF9999;
display: none;
padding: 3px;
}
This CSS will hide the ToolTipDiv by default, and give it a nice look. Technically, all you need is the positioning and display, but looking good is half the reason to use your own tooltips.
For more on the jQuery side, we need to do a couple things. First we need to show and hide the div when we mouse over an anchor tag. To do this, we are going to find all the anchor tags and iterate through each one:
$("body").append("<div id='ToolTipDiv'></div>");
$("a[title]").each(function() {
});
});
Using a[title], we can select only the anchors with title attribute. This way we do not end up with any empty tooltips. I mean who wants a tiny empty box floating around?
Inside our loop, we need to set the hover in and hover out events to show/hide the ToolTip. For extra effect, we are going to use the fadeIn and fadeOut functions to do the show/hide, which give use a little more fanciness. For the hover in/out event, we use the hover event helper jQuery provides:
$("body").append("<div id='ToolTipDiv'></div>");
$("a[title]").each(function() {
$(this).hover(function(e) {
$("#ToolTipDiv")
.stop(true,true)
.fadeIn("fast");
}, function() {
$("#ToolTipDiv")
.stop(true,true)
.fadeOut("fast");
});
});
});
What this jQuery code is doing is taking each anchor, then adding a hover event. This helper provides a hover over, and a hover out event. The first argument is the hover over function, and the second is the hover out. In the over, we "fade in" the ToolTip, and of course we "fade out" when we mouse out. We are also adding a stop command, so the current animation stops before playing a new fade in/out. This way we avoid conflicting animations, which can cause flashing.
As of right now, all we are doing is showing and hiding the tooltip. We need to reposition it as well. To do this, we are going to use a mousemove event, and the position of the mouse. Luckily, jQuery makes this easy:
$("body").append("<div id='ToolTipDiv'></div>");
$("a[title]").each(function() {
$(this).hover(function(e) {
$().mousemove(function(e) {
var tipY = e.pageY + 16;
var tipX = e.pageX + 16;
$("#ToolTipDiv").css({'top': tipY, 'left': tipX});
});
$("#ToolTipDiv")
.stop(true,true)
.fadeIn("fast");
}, function() {
$("#ToolTipDiv")
.stop(true,true)
.fadeOut("fast");
});
});
});
We are taking the anchor, and adding a mousemove event that repositions the tooltip div using CSS. We also capture the mouse coordinates, then offset them so they are roughly at the corner of your pointer. You get these mouse coordinates using pageY and pageX, which are properties of the object returned during the event.
With the position correct, all we have to do is fill the tooltip with the text from the title attribute. But in order to keep the browser's tooltips from working, we are going to remove the attribute when we mouse over, then reset it when we mouse out. To reset it, we simply use the Tooltip div's text:
$("body").append("<div id='ToolTipDiv'></div>");
$("a[title]").each(function() {
$(this).hover(function(e) {
$().mousemove(function(e) {
var tipY = e.pageY + 16;
var tipX = e.pageX + 16;
$("#ToolTipDiv").css({'top': tipY, 'left': tipX});
});
$("#ToolTipDiv")
.html($(this).attr('title'))
.stop(true,true)
.fadeIn("fast");
$(this).removeAttr('title');
}, function() {
$("#ToolTipDiv")
.stop(true,true)
.fadeOut("fast");
$(this).attr('title', $("#ToolTipDiv").html());
});
});
});
Using the title attribute, we maintain the tooltip functionality, even when javascript is disabled. They won't be our custom ones, but the browser may still give you it's tooltip.
Using CSS and jQuery, the possibilities for our ToolTips is endless. You can style it, or add the tooltip to any element you want. There are even some easy to use jQuery plugins for more complex tooltips.
Well, now you know how to make simple custom tooltips using jQuery. Fortunately, it has been pretty simple. This is it for now, but just remember: if you need coding help, just Switch On The Code. If you have any questions feel free to leave a comment or check out our forums.
06/22/2009 - 09:40
Waste of time - if the tipped word is near the right hand side of the page the tip appears off screen. Same with if the word is at the bottom of the window. You need to adjust position depending where the tipped word is.
06/22/2009 - 10:20
Good point, we'll try and get the code updated and re-posted as soon as possible.
08/20/2009 - 02:40
great tooltip function, i'm using it in my future projects. thanks
08/27/2009 - 19:11
IE seems to show both tool tips, the new one plus the original.
01/12/2010 - 06:15
Nice script, but same bug here
In ie8 the original tooltip is also appearing on mouseover
02/09/2010 - 04:20
Had the same problem with ie6 shows both tooltips.
A possible fix in the js:
$(document).ready(function() {
$("body").append("");
$("a[title]").each(function() {
var text=$(this).attr('title');
$(this).removeAttr('title');
$(this).hover(function(e) {
$().mousemove(function(e) {
var tipY = e.pageY + 16;
var tipX = e.pageX + 16;
$("#ToolTipDiv").css({'top': tipY, 'left': tipX});
});
$("#ToolTipDiv")
.html(text)
.stop(true,true)
.fadeIn("fast");
}, function() {
$("#ToolTipDiv")
.stop(true,true)
.fadeOut("fast");
});
$(this).attr('title', $("#ToolTipDiv").html());
});
});
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.