views:

975

answers:

4

Hi everyone,

I'm trying to build a hoverable Jquery tooltip. This tooltip should appear when I hover over some element, and stay put if I choose to hover over the tooltip itself too. The tooltip should disappear only if I hover away from the original element or from the tooltip body.

Based on an example I found, I managed to create this behavior, but since I'm new to Jquery, I'd be glad to hear your comments about improving the function.

The code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head>
<script src="jquery-1.2.6.min.js"></script>
<style>
#tooltip{
       position:absolute;
       border:1px solid #333;
       background:#f7f5d1;
       padding:2px 5px;
       color:#333;
       display:none;
       text-align: left;
}

</style>


</head>
<body>

<script type="text/javascript">
jQuery.fn.extend({
       'tooltip': function(text){
                               xOffset = 10;
                               yOffset = 20;

                               var that = this;
                       $(this).mouseover(function(e){
                               this.t = text;
                               $("body").append("<div id='tooltip'>"+ this.t +"</div>");
                               $("#tooltip")
                                       .css('position', 'absolute')
                                       .css("top",(e.pageY - xOffset) + "px")
                                       .css("left",(e.pageX + yOffset) + "px")
                                       .fadeIn("fast");
                   });
                       $(this).mouseout(function(){
                               that.hide_ff = setTimeout('$("#tooltip").hidetooltip()', 1000);
                               $("#tooltip").hover(function(){
                                       clearTimeout (that.hide_ff);
                               },
                               function(){
                                       $("#tooltip").hidetooltip()
                               });

                               //$("#tooltip").hidetooltip()
                   });
                       $(this).mousemove(function(e){
                               $("#tooltip")
                                       .css("top",(e.pageY - xOffset) + "px")
                                       .css("left",(e.pageX + yOffset) + "px");
                       });
       },

       'hidetooltip': function()
       {
               var that = this;
               $(this).remove();
               if (that.hide_ff)
               {
                       clearTimeout (that.hide_ff);
               }
       }
});

</script>
<a id="fff">ToolTip</a>

<div id="tooltip_share_text" style="display:none">
       <div style="width: 100px;">
       This is a Tooltip.
       <br/>
       <a href="javascript:void(0)" onclick="alert('boo')"> Click Me</a>
       </div>
</div>
<script type="text/javascript">
$(document).ready(function() {
       $("#fff").tooltip($('#tooltip_share_text').html());
});
</script>

</body>
</html>


Two things bother me most:

  1. I needed to extend Jquery with 2 function (tooltip and hidetooltip), i would like to achieve the same behavior with only one extension but I didn't succeed in accomplishing this.
  2. The use I made of "that.hide_ff" just doesn't seem right. Once again, I think this variable should belong to a "tooltip" object, but if I am not mistaken it is attached to the Jquery object itself.

In addition, I would be happy to hear any other improvements...

Thanks in advance, Gordi

+2  A: 

There is a tooltip plugin for JQuery. If you'd rather roll your own, I'm sure you can get ideas by looking at what they have done.

Tim Scott
A: 

Thanks Tim, I am aware of the plugin you attached but it didn't seem to fit my needs. Having said that, I'd be sure to check out their code to get some ideas.

If anyone could help me specifically with the 2 issues I raised in the original post it would be highly appreciated :-)

Thanks again...

A: 

I'm not sure if you're still interested in this... it's been almost a year o.O

But I modified your code a little:

  • I got rid of the hidetooltip (the extra extension)
  • Using the that.hide_ff is fine, so I didn't change it
  • The tooltip pops up at the end of the link and doesn't move with the mouse - it looks cleaner I think.
  • Switched the xOffset and yOffset
  • Commented out the original mousemove code so you can change it back if you don't like it.
jQuery.fn.extend({
 'tooltip': function(text){
  xOffset = 10;
  yOffset = 20;

  var that = this;
  $(this).mouseover(function(e){
   this.t = text;
   $("body").append(""+ this.t +"");
   $("#tooltip")
    .css('position', 'absolute')
    .css("top",(this.offsetTop + yOffset) + "px") /* .css("top",(e.pageY - xOffset) + "px") */
    .css("left",(this.offsetLeft + this.offsetWidth) + "px") /* .css("left",(e.pageX + yOffset) + "px") */
    .fadeIn("fast");
  });
  $(this).mouseout(function(){
   that.hide_ff = setTimeout('$("#tooltip").remove()', 500);
   $("#tooltip").hover(function(){
    clearTimeout (that.hide_ff);
    },
    function(){
     $("#tooltip").remove();
    }
   );
  });
/*
  $(this).mousemove(function(e){
   $("#tooltip")
    .css("top",(e.pageY - xOffset) + "px")
    .css("left",(e.pageX + yOffset) + "px");
  });
*/
 }
});
fudgey
A: 

I've found that qTip has met all of my tooltip needs:

Alex