Experimental jQuery Tooltips

25 August 2009| 12 Comments| Print

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.

So here’s the JS

$(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 HTML

The following combination of attributes can be added to any link to create a tooltip.

  • rel=”tooltip” | this enables the link as a tooltip without this no tooltip would be show for that specific link
  • class=”ajax” | if you want to call another page using ajax into the tooltip you need to add this class aswell as add the attribute ‘ajax’ with the link to the page you want to call
  • ajax=”AJAX_URL_HERE” | this is where you specify the link to the page that you want to pull in using ajax.
  • content=”Tooltip content” | this is where you put your main tooltip text including any image using regular html
<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. :)

demodownload


Share:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Google Bookmarks
  • DZone
  • Reddit
  • Netvibes

12 Comments

  • Balaji

    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.

  • Balaji

    Very nice! I’ve been wondering aboutJquery lately. Do you have any thoughts about Drop and down using Jquery

  • CSS Brigit | Experimental jQuery Tooltips

    Experimental jQuery Tooltips…

    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.

  • Jake

    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.

  • 網站製作學習誌 » [Web] 連結分享

    [...] Experimental jQuery Tooltips [...]

  • Tutorial City

    It seems that the last tooltip doesn’t appear on the right spot on mouseenter, just on mousemove.
    Very cool tutorial ;)

  • Matt

    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.

  • Ashley

    @Matt thanks for that! i’ll be sure to update the code with your modification :)

  • Joe

    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?

  • Ashley

    @joe good to hear you found it useful, you could always use ‘name’ instead of content or ‘title’

  • Rafa

    @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

  • Christian

    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

Leave a comment...

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled site. To get your own globally-recognized-avatar, register at Gravatar.

Your Ad Here