Experimental jQuery Tooltips

Experimental jQuery Tooltips

Posted on August 25, 2009 by Ashley

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

This entry was posted in CSS, Design, Downloads, Javascript, Tutorials, Web Tools, jQuery and tagged , , , , . Bookmark the permalink.
Comments
12 discussions around Experimental jQuery Tooltips
  1. Balaji says:

    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.

  2. Balaji says:

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

  3. Pingback: CSS Brigit | Experimental jQuery Tooltips

  4. Jake says:

    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.

  5. Pingback: 網站製作學習誌 » [Web] 連結分享

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

  7. Matt says:

    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.

  8. Ashley says:

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

  9. Joe says:

    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?

  10. Ashley says:

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

  11. Rafa says:

    @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

  12. Christian says:

    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 Reply

Your email address will not be published. Required fields are marked *

*

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

Subscribers
1,250
Twitter
510
Comments
1,207
Posts
125