Papermashup

Subscribe


Tweets


"RT @bethgordon: .@Tesco Please rethink archaic 'Approved by Mums'. Pretty sure it's not just mothers who make childcare decisions. https://…"

@ashleyford 3 days ago

"Is there ever a time that @DFS doesn't have a sale on?"

@ashleyford 7 days ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Experimental jQuery Tooltips

AshleyAshley

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.


<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

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 15
  • Dorababu
    Posted on

    Dorababu Dorababu

    Reply Author

    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.


  • Rob
    Posted on

    Rob Rob

    Reply Author

    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


  • TDP Workspace
    Posted on

    TDP Workspace TDP Workspace

    Reply Author

    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.


  • Christian
    Posted on

    Christian Christian

    Reply Author

    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


  • Rafa
    Posted on

    Rafa Rafa

    Reply Author

    @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


  • Joe
    Posted on

    Joe Joe

    Reply Author

    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
      Posted on

      Ashley Ashley

      Reply Author

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


  • Matt
    Posted on

    Matt Matt

    Reply Author

    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
      Posted on

      Ashley Ashley

      Reply Author

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


  • Tutorial City
    Posted on

    Tutorial City Tutorial City

    Reply Author

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


  • Jake
    Posted on

    Jake Jake

    Reply Author

    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.


  • Balaji
    Posted on

    Balaji Balaji

    Reply Author

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


  • Balaji
    Posted on

    Balaji Balaji

    Reply Author

    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.