I’ve never written a jQuery tooltip script before so i thought i’d give it a go and write my own. Its very much in the experimental stage but i thought i’d open it up to see what the community has to say about it. I had the most trouble with the ajax tooltip and getting the content to display in the tip. I know there are plenty of jQuery tooltip scripts out there but i really wanted to see how they were made, and the best way to find out is to build one for your self.
$(document).ready(function(){
$('[rel=tooltip]').bind('mouseover', function(){
if ($(this).hasClass('ajax')) {
var ajax = $(this).attr('ajax');
$.get(ajax,
function(theMessage){
$('<div class="tooltip">' + theMessage + '</div>').appendTo('body').fadeIn('fast');});
}else{
var theMessage = $(this).attr('content');
$('<div class="tooltip">' + theMessage + '</div>').appendTo('body').fadeIn('fast');
}
$(this).bind('mousemove', function(e){
$('div.tooltip').css({
'top': e.pageY - ($('div.tooltip').height() / 2) - 5,
'left': e.pageX + 15
});
});
}).bind('mouseout', function(){
$('div.tooltip').fadeOut('fast', function(){
$(this).remove();
});
});
});
The following combination of attributes can be added to any link to create a tooltip.
<li><a href="#" alt="Text Tooltip" rel="tooltip" content="<span>Text Title</span><br/> This is an example of a text tooltip with jquery">Text Tooltip</a></li> <li><a href="#" alt="Image Tooltip" rel="tooltip" content="<span>Image Title</span><br/> <img src='http://papermashup.com/demos/jquery-gallery/images/t2.png' width='120' height='120' class='tooltip-image'/> This is an example of an image tooltip with jquery, with a little bit of text.<br/> Remember you can follow me on twitter just search: ashleyford">Image Tooltip</a></li> <li><a href="#" alt="Image Tooltip" rel="tooltip" content="<span>Iframe Tooltip</span><br/> <iframe src='http://google.com' width='250px' height='100px' frameborder='0' scrolling='0'></iframe>">Iframe Tooltip</a></li> <li><a href="#" class="ajax" alt="Image Tooltip" rel="tooltip" ajax="ajax.php?id=1823472">Ajax Tooltip</a></li>
I’m posting this purely as an experiment, and this code hasn’t been tested in all browsers so is very much in the alpha stage. I’d love to hear your feedback on what or how you would do it.
Nice one..
What should I do if I would like to do like this, on clicking I would like to pop up a tool tip with close button . I would like to implement and hover and click together on the same icon or control we have taken.
I think it’s good to create a plugin using prototyping, allowing for more flexibility of the plugin, like this one here: http://www.websanova.com/plugins/websanova/tooltip
Hello guys awesome job anyways we got a problem with W3C validation with the “content” atribute anyways is easy to fix but for the who’s just copy and apply keep it in mind.
To mask the tooltip with its creation
To add to the class
.tooltip{
visibility:hidden;
}
To add to the script
$(this).bind(‘mousemove’, function(e){
$(‘div.tooltip’).css({
‘top’ : e.pageY – ($(‘div.tooltip’).height() / 2) – 5,
‘left’: e.pageX + 15
})
.css(‘visibility’, ‘visible’);
});
Thank you for this excellent work
Christian
@joe You could use the [rel^='tooltip'] selector to make it work and have a link like this:
<a href="#" alt="Text Tooltip" rel="tooltip Text Title This is an example of a text tooltip with jquery">Text Tooltip
Then you can remove the “tooltip” part and voila! Successful validation, but i don’t know if it would be a huge impact on performance
Nice script, I used on my site and it works well – only issue is it doesnt validate because of the content attribute – any workarounds for that?
The mousemove function, which controls the position of the tooltip, isn’t bound until mouseover fires. The result is if the user moves his mouse slowly or lands on the wrong spot the tooltip will appear at the bottom of the page until he moves his mouse again.
I altered your code by styling the div to “display:none” inside of mouseover and then changing it to “display:block” inside of mousemove. Now it works like a champ.
It seems that the last tooltip doesn’t appear on the right spot on mouseenter, just on mousemove.
Very cool tutorial
I was thinking if this will gonna work for comments but i think it will but have a lots of code.
Example: the name of the commentor will only display like a list and the body of comment will show if you point it using this tooltip.
Very nice! I’ve been wondering aboutJquery lately. Do you have any thoughts about Drop and down using Jquery
Wow, I hadn’t thought about it that way before. Good write up, very clearly written. Have you written previously about Experimental jQuery Tooltips? I’d love to read more.
Designer and web developer, Co-founder and Technical Director at Harkable.com London. Previously I worked at InMobi, Spotify and MySpace. Interests include photography and making short videos. Also an avid F1 fan.
Follow us on Twitter and get in-stream updates.
Subscribe to all the Papermashup tutorials and articles straight to your RSS reader.
Sign up and get all the Papermashup tutorials straight to your inbox.
15 discussions around Experimental jQuery Tooltips