Hey guys! I've been trying to be a bit more active lately and provide you all with some decent articles. So, because of this, I took some time off my busy schedule last night to sit down and teach myself how to create jQuery plugins. I was trying to figure out what I could develop that would be useful when I was reminded of how many times I tried to implement a javascript-based tooltip solution.
There are plenty of these plugins out there that are dedicated to display tooltips, but I wanted a dirt-simple implementation that didn't require me to mull about CSS code and compatibility issues.
So, I wrote a little plugin that addresses all these inconvenient issues; qTips.
Why 'qTips'? Well, you can quickly add tooltips. So, it's actually quick tips, but qTips sounds catchier.
A Demonstration
Actions speak louder than words, so before I go into this plugin's details, I thought I'd show you all a proof-of-concept demonstration. Just hover your mouse pointer over the boxes and you'll see what happens. A pretty little tooltip will fade in and out with your mouse movements.
For more people, this is all they need.
Implementation
First, you might want to download the plugin package. You'll notice that it comes with the same demo page that I previously linked to.
This plugin is implemented like any other. After you link to your jQuery library's location, you place the following immediately after:
<script src="/path/to/plugin/jquery.qtips.js" type="text/javascript"></script>
Since we need to style the basic implementation of the tooltip, we should add the following CSS to one of your stylesheets:
div.qtip-wrapper {
z-index: 999;
text-align: center;
position: absolute;
font-size: .9em;
width: 250px;
background: transparent url(arrow.png) no-repeat scroll center bottom;
color: #fff;
padding-bottom: 5px;
display: none
}
div.qtip-wrapper div {
background: #333 none;
padding: 5px;
font: normal normal .9em/1em "Arial", verdana, sans-serif;
}
You can substitute the above background image with anything you want, so long as the position information is still intact.
Next, you just implement the plugin and assign it to a selection of some kind. Take the following markup located within the <body> element of a document, for instance:
<body>

Simple enough, but you'd like to include a tooltip that displays some image meta data when someone hovers their mouse over the image. To do this, you just add the following within your document:
<script type="text/javascript">
$(document).ready(function()
{
$('img#product-image-5').qtip({content:'Here is some image info...'});
});
</script>
This tooltip is now applied to every element within your document that matches the specified selector. Of course, while using jQuery, you can apply a tooltip to just about anything within your page with a single line of code.
Configuration
While this plugin may be a simple tooltip implementation that doesn't mean it lacks functionality. There are several configuration options that you can choose from to fine-tune your tooltips so they display exactly how you want them to. Here is a quick breakdown of the current configuration items and their default values:
container
default: 'qtip'
usage: $(selector).qtip({container:'qtip'});
This allows you to specify a default prefix for the ids assigned to the tooltip elements themselves. Since there may be multiple tooltips displayed at any given time, an incrementor is used and suffixed to the end of the container name to prevent naming conflicts.
content
default: blank
usage: $(selector).qtip({content:'Why, good DAY to you, sir!'});
This is the content you want to display within your tooltip. It can be a string, html markup or even a DOM object.
position
default: 'center'
usage: $(selector).qtip({position:'center'});
choices: left, right, center, bottom
This setting defines the position of the tooltip relative to the matched element.
nudge_top
nudge_top: 10
usage: $(selector).qtip({nudge_top:5});
How many pixels above the matched element you'd like the tooltip to be displayed.
nudge_left
nudge_left: 0
usage: $(selector).qtip({nudge_left:5});
How many pixels to the left of the matched element you'd like the tooltip to be displayed.
Callback Functions
qTips comes with 4 built-in callback functions that are triggered during various phases of the tooltip display. All functions pass the object of the currently matched element as well as the object of the current tooltip.
preRender(e, tip)
usage: $(selector).qtip({preRender:function(e, tip){}});
This is triggered immediately after your mouse enters the boundaries of the matched element, but before anything is actually processed for display.
postRender(e, tip)
usage: $(selector).qtip({postRender:function(e, tip){}});
This is triggered last in the tooltip process; immediately after the tooltip fades away.
onShow(e, tip)
usage: $(selector).qtip({onShow:function(e, tip){}});
This is triggered when the tooltip is pushed out for display.
onHide(e, tip)
usage: $(selector).qtip({onHide:function(e, tip){}});
This is triggered when the tooltip disappears.
Conclusion
This is my very FIRST jQuery plugin, so please, be gentle. I could always use some suggestions and bug reports. I'd like this to be useful for everyone and not just me, so if you'd like to see something that can easily be implemented or you find an instance where it's not working properly, just send me an email or post a comment here.
This has actually been quite a pleasant experience. You don't fully realize the potential of jQuery until you actually take the time to build something off of the framework. Good stuff all around!
I hope you enjoy using it just as much as I did writing it!
qTips for all images:
Thanks to Nick for his lovely comment below, I have decided to post a quick snippet on how you can add a qTip to every image on a page. Just use the following code:
$('img').qtip(
{
wrapper: 'body',
position: 'right',
onShow: function(e, tip)
{
$('div', tip).empty().append('
Download
In case you missed it, here is the download link!
You can also find the official qTips project page here on jQuery's plugin repository.
Comments [0]