Actions

J1.5

Difference between revisions of "How to add tooltips to your Joomla! website"

From Joomla! Documentation

(More Information)
m (add review tag)
Line 1: Line 1:
{{underconstruction}}
+
{{review}}
 
===Tooltips Overview===
 
===Tooltips Overview===
 
A tooltips is a piece of text that pops up when you hover the mouse over a region on a website. If you use the Joomla! back end, for example, tooltips are used to help explain the action of different parameters, as shown in the screenshot below:  
 
A tooltips is a piece of text that pops up when you hover the mouse over a region on a website. If you use the Joomla! back end, for example, tooltips are used to help explain the action of different parameters, as shown in the screenshot below:  

Revision as of 12:38, 10 February 2009

Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.
Copyedit.png
This Page Needs Your Help

This page is tagged because it NEEDS REVIEW. You can help the Joomla! Documentation Wiki by contributing to it.
More pages that need help similar to this one are here. NOTE-If you feel the need is satistified, please remove this notice.


Contents

Tooltips Overview

A tooltips is a piece of text that pops up when you hover the mouse over a region on a website. If you use the Joomla! back end, for example, tooltips are used to help explain the action of different parameters, as shown in the screenshot below:

Screen tooltip example 20090208.png

Tooltips are a great way to give the user access to information without using up space on the screen. You can use basic tooltips just by adding a "title" attribute to a tag. However, this doesn't give you the option to style the tooltips. With Joomla! version 1.5, it is very easy to add styled tooltips to your site. This tutorial will show you how.
Note: If you are converting old code, make sure it doesn't use the prototype.js file. That file will keep the tooltips from working.

Activate Tooltip Behavior

To start, you must activate the tooltip behavior. This is done with the following line of code:

JHTML::_('behavior.tooltip');

Note that you only put this line in once per file. A good place is right after the defined('_JEXEC') or die('Restricted access'); line. If you are curious about what this line does, it inserts the following JavaScript code in the heading of the HTML page:

<script type="text/javascript">
    window.addEvent('domready', function(){ 
       var JTooltips = new Tips($$('.hasTip'), 
       { maxTitleChars: 50, fixed: false}); 
    });
</script>

This allows tooltips to function, as outlined below.

Create a Tooltip with the JHTML::tooltip Method

One way to create a tooltip is using the JHTML::tooltip method. The API documentation is available at http://api.joomla.org/Joomla-Framework/HTML/JHTML.html#tooltip. The method definition is shown below.

void tooltip (string $tooltip, [string $title = ''], [string $image = 'tooltip.png'], 
              [string $text = ''], [string $href = ''], [boolean $link = 1])

The brackets mean the parameter is optional. The first parameter, "$tooltip", is the only required parameter. The equals specifies the default if you do not pass that parameter. For example, the image file 'tooltip.png' is the default image.

The following is a basic tooltip. Keep in mind that the image variable must be in reference to 'includes/js/ThemeOffice'. Use the prefix '../../../' to reference from the root of the Joomla installation.

JHTML::tooltip('This is the tooltip text', 'Tooltip title', 'tooltip.png', '', 
               'http://www.joomla.org');

The above will make a tooltip using the default "tooltip.png" image, as shown below.

Tooltip tutorial example screenshot 20090208.png

Clicking the image will take you to http://www.joola.org, since that is the "$href" argument.

This code is almost the same as above except the image will not be a link (because the "$href" argument is omitted).

JHTML::tooltip('This is the tooltip text', 'Tooltip title', 
               'tooltip.png', '', '', false);

A tooltip can be attached to an image or to text. For example, this code

echo JHTML::tooltip('This is a tooltip attached to text', 'Text Tooltip Title', 
                    '', 'Hover on this text to see the tooltip');

will show a tooltip attached to text, as shown below.

Tooltip tutorial example screenshot2 20090208.png

Note that specifying the "$text" parameter will override any image you have passed to tooltip.

Create a Tooltip Using a CSS Class Name

If we look at the HTML page source generated by the JHTML::tooltip function above, here is what we see:

<span class="editlinktip hasTip" title="Text Tooltip Title::This is a tooltip attached to text" 
      style="text-decoration: none; color: #333;">Hover on this text to see the tooltip</span>

This function generates an HTML "span" tag with the classes "editlinktip" and "hasTip" and an attribute called "title". Notice that the the "title" attribute has two parts, <tooltip title>::<tooltip text> (for example, "Tooltip Title::This is the tooltip text"). You can create a tooltip with only text or with only a title. The title has implications for styling, as we'll see below.

The Javascript inserted by the JHTML::('behavior_tooltip') command we entered earlier picks up this tag based on the class called "hasTip". So, a second way of creating a tooltip is to simply create a tag with a class called "hasTip" and an attribute of "title". For example, this code

<span class="hasTip" title="Text Tooltip Title::This is a tooltip attached to text">
   Hover on this text to see the tooltip</span>

will produce exactly the same effect as the JHTML::tootip() example above.

Adding CSS Styling to the Tooltip

The default tooltips, whether using the JHTML::tooltip method or class method, use the following three CSS classes: .tool-tip, .tool-title, and .tool-text.

Here are the default styles.

/* Tooltips */
.tool-tip {
   float: left;
   background: #ffc;
   border: 1px solid #D4D5AA;
   padding: 5px;
   max-width: 200px;
}
 
.tool-title {
   padding: 0;
   margin: 0;
   font-size: 100%;
   font-weight: bold;
   margin-top: -15px;
   padding-top: 15px;
   padding-bottom: 5px;
   background: url(../images/selector-arrow.png) no-repeat;
}
 
.tool-text {
   font-size: 100%;
   margin: 0;
}

If you have a custom CSS file, copy this code and alter it to your liking. Note that the .tool-title class uses by default the "selector-arrow.png" image. This is what gives the outline of the tooltip the little pointer in the upper left of the tooltip box. If you leave out a title in your tooltip, you will just get a rectangle, without the pointer.

Customizing Your Yooltips

Ok, now it's time to have some fun. The JHTML::_('behavior.tooltip') can take two optional parameters. The first parameter is the name of the CSS class that will be used to identify the tooltip. As we saw earlier, this defaults to "hasTip". The second optional parameter is an array of parameters that you can use to fine-tune the tooltip functionality. These are explained below.

  • maxTitleChars: The maximum length of the title (the part of the "title" attribute before the "::")
  • showDelay, hideDelay: The time to delay showing or hiding the tooltip, in milliseconds. Default is 100.
  • className: The first part of the class name used to style the actual tooltip. The default, as we saw, is "tool", and the full class names by default are "tool-tip", "tool-title', and "tool-text". So, for example, if we set this to "custom", we would style the classes "custom-tip", "custom-title", and "custom-text".
  • fixed: Whether or not to have the tooltip move with the mouse. If set to "true", the tooltip will be centered below the text or image. If set to "false", the tooltip will move with the mouse. Default is "false".
  • onShow, onHide: Functions to call for the onShow and onHide events. We'll see an example below where we can use these to create a fade-in and fade-out for the tooltip.

In the next section, we'll show how to use these parameters with some examples.

Tooltip With Different Classes

In this example, we will create a tooltip using different selector and tooltip styling classes. Why might we want to do this? One reason would be if we had different types of tooltips and we wanted to style them differently.

Here is the code to create the tooltip Javascript function:

$toolTipArray2 = array('className'=>'custom2');
JHTML::_('behavior.tooltip', '.hasTip2', $toolTipArray);

In this code, we are creating an array with just one element -- to set the "className" parameter to "custom2". This means that, to style these tooltips, we would use the classes custom2-tip, custom2-title, and custom2-text. Then we call the method with the arguments ".hasTip2" and the array "$toolTipArray". It can be a little confusing, because we have two CSS classes at work here. The class ".hasTip2" is used in the HTML tag. That is how the Javascript program identifies the tooltips to operate on. The class "custom2" is used as the first part of the three CSS classes to use to style the actual tooltip itself.

To use this new tooltip, we could use the following code:

<span class="hasTip2" 
        title="My Tooltip Title :: Tooltip text for hasTip2 class with custom2 style classes.">
        hasTip2 and custom2 example</span>

Since we set the class to "hasTip2", this tag will be processed by the new version of the tooltip program. So, if we use both the default tooltip ("hasTip" class) and the new tooltip ("hasTip2" class) in the same page, we can style them differently using "tool-" styles for the default and "custom2-" styles for the "hasTip2" tooltips.

Next, let's look at a more complex example, shown below.

$toolTipArray = array('className' => 'custom2', 'showDelay'=>'500', 
   'hideDelay'=>'500', 'fixed'=>'true',
   'onShow'=>"function(tip) {tip.effect('opacity', 
      {duration: 500, wait: false}).start(0,1)}", 
   'onHide'=>"function(tip) {tip.effect('opacity', 
      {duration: 500, wait: false}).start(1,0)}");
JHTML::_('behavior.tooltip', '.hasTip2', $toolTipArray); ?>

Again, we are building an array to pass to the JHTML::_('behavior.tooltip') function. Again, we are setting the "className" parameter to "custom2". We are also setting the "showDelay" and "hideDelay" parameters to 500 milliseconds (.5 seconds). So the tooltip will show only after the mouse has hovered for this amount of time.

Notice that we are setting the parameter "fixed" to "true". This means that the tooltip will not move with the mouse. Instead, it will always show centered below the tooltip text or image.

Finally, the tricky part. We are creating functions to pass in to the "onShow" and "onHide" events. These functions cause the tooltip to fade in and fade out over the space of 500 milliseconds. These functions are based on sample code in the MooTools demo tutorial. Click on the link called "js code" and look at the code for the "Tips2" demo. We have just taken the code from the initialize function and incorporated it into the "onShow" and "onHide" functions.

The last line of code above creates the actual Javascript code in our Joomla! document. If we show the page and view source, we see the following:

window.addEvent('domready', function(){ 
   var JTooltips = new Tips($$('.hasTip2'), {
      maxTitleChars: 50, showDelay: 500, hideDelay: 500, className: 'custom2', 
         fixed: true, 
      onShow: function(tip) {tip.effect('opacity', {duration: 500, wait: false}).start(0,1)}, 
      onHide: function(tip) {tip.effect('opacity', {duration: 500, wait: false}).start(1,0)}
   }); 
});

So we can see the effect of the parameters that we passed to the JHTML::_('behavior.tooltip') method.

Using an External Javascript File

There are times when you might prefer to work with a separate Javascript file. This gives you complete flexibility to use all of the mootools capabilities. To do this:

  1. Create the separate Javascript file and save it somewhere in your Joomla! site (for example, "templates/<your template>/js/yourjsfile.js".
  2. Use the JHTML::script function to add this script to your document. For example:
   $filename = 'testtooltip.js'; // this file is used for class="hasTip3" titles
   $path = 'templates/rhuk_milkyway/js/'; // path to the file
   // true means MooTools will load if it is not already loaded
   JHTML::script($filename, $path, true);

This will load your script into the document and make sure that the MooTools library is available.

Complete Code Example

Here is an example that incorporates all of the elements discussed in this tutorial. So you can see the code in action, we will create an override of the "mod_stats" Module and add our test code there. We will use the rhuk_milkyway template.

To set up the template override, create a folder called "templates/rhuk_milkyway/html/mod_stats" and copy the file "modules/mod_stats/tmpl/default.php" to this new folder.

After line 2 of this file (defined('_JEXEC') or die('Restricted access');), insert the following:

JHTML::_('behavior.tooltip');
$toolTipArray = array('className' => 'custom2', 'showDelay'=>'500', 'hideDelay'=>'500', 'fixed'=>'true'
, 'onShow'=>"function(tip) {tip.effect('opacity', {duration: 500, wait: false}).start(0,1)}"
, 'onHide'=>"function(tip) {tip.effect('opacity', {duration: 500, wait: false}).start(1,0)}");
JHTML::_('behavior.tooltip', '.hasTip2', $toolTipArray);  // this is used for class="hasTip2" titles
$filename = 'testtooltip.js'; // this file is used for class="hasTip3" titles
$path = 'templates/rhuk_milkyway/js/';
JHTML::script($filename, $path, true); // MooTools will load if it is not already loaded

Here we are creating the Javascript functions we will need later. The first line is the simple default declaration that will use the ".hasTip" and "tool-" CSS classes.

The next 5 lines create the more customized function that includes the fade-in and fade-out. It uses the ".hasTip2" to create the tip and the classes starting with "custom2-" to style the tooltip.

The last 3 lines reference the external Javascript file, which will be created below. The class information will be contained in that file.

Now, time for some more coding. At the end of the "modules/mod_stats/tmpl/default.php" file, add the code shown below:

<h1>Tooltip Examples</h1>
<?php echo JHTML::tooltip('This is the tooltip text', 'Tooltip title', 
        'tooltip.png', '', 'http://www.joomla.org');?>
<br/><br/>
<?php echo JHTML::tooltip('This is a tooltip attached to text', 'Text Tooltip Title', 
                                                        '', 'Hover on this text to see the tooltip');?>
<br/><br/>                                                      
<a class="hasTip" title="My Title::Tooltip on 'a' tag text using default 'hasTip' class" 
        href="http://www.joomla.org">Tooltip on a link example</a> 
<br/><br/>
<span class="hasTip2" 
        title="My Tooltip Title :: Tooltip text for hasTip2 class with custom2 style classes.">
        hasTip2 and custom2 example</span>
<br/><br/>
<span class="hasTip3" 
        title="hasTip3 Title::This is using class 'hasTip3' using external JS script.">
           hasTip3 hover text</span>
<br/><br/>

The first two examples using the JHTML::tooltip function to create the tooltip HTML code.

The third example shows an "a" tag with a tooltip. This illustrates that you can put tooltips on most HTML tags, not just "span" tags. Again, it is using the "hasTip" class, so it will be controlled by the Javascript above called with the ".hasTip" argument and will be styled with the default "tool-" styles.

The fourth example shows a "span" tag using the "hasTip2" class. So it will be styled using the "custom2-" classes and will have the "showDelay" and "hideDelay" of 500 milliseconds and have "fixed" equal to "true" (so the tip will not move with the mouse). Also, it uses the special fade-in and fade-out effects.

The last example will use the Javascript code from the external file, shown below.

Now, some more coding. Create a JavaScript file called "testtooltip.js" and save it into the folder "templates/rhuk_milkyway/js":

window.addEvent('domready', function(){
   //do your tips stuff in here...
   var zoomTip = new Tips($$('.hasTip3'), {
      className: 'custom3', //this is the prefix for the CSS class
      initialize:function(){
         this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
              },
         onShow: function(toolTip) {
            this.fx.start(1);
         },
         onHide: function(toolTip) {
            this.fx.start(0);
         }
   });
});

This code is taken from the MooTools demo site referenced above. Notice that will operate on the "hasTip3" class and will use style classes starting with "custom3-". This also has the fade-in and fade-out functionality.

Finally, add this to the end of your "templates/rhuk_milkyway/css/template.css" file:

.custom2-tip {
        color: #000;
        width: 130px;
        z-index: 13000;
}
 
.custom2-title {
        font-weight: bold;
        font-size: 11px;
        margin: 0;
        color: #024A68;
        padding: 8px 8px 4px;
        background: #3CA0D0;
        border-bottom: 1px solid #024A68;
}
 
.custom2-text {
        font-size: 11px;
        padding: 4px 8px 8px;
        background: #63ADD0;
}
 
.custom3-tip {
        color: #000;
        width: 130px;
        z-index: 13000;
}
 
.custom3-title {
        font-weight: bold;
        font-size: 11px;
        margin: 0;
        color: #3E4F14;
        padding: 8px 8px 4px;
        background: #C3DF7D;
        border-bottom: 1px solid #B5CF74;
}
 
.custom3-text {
        font-size: 11px;
        padding: 4px 8px 8px;
        background: #CFDFA7;
}

Here we are just adding different colors for the "custom2-" and "custom3-" styles so you can see the effect of the different tooltip code.

Notice that .custom2-title and .custom3-title classes do not have a background property. So these tooltips will be a box without the neat little arrow pointing at the mouse. If you wanted, you could create an image similar to "templates/system/images/selector-arrow.png" of the right color and add that as a background to these classes.

Example Code Screenshots

Here are some screenshots taken from the sample code above. The first screenshot shows the modified "mod_stats" modules with our sample code.

Screen tooltip example2 20090210.png

The screenshot below shows the fade-in effect for the ".hasTip2" tooltip. Notice also that the tooltip is centered below the text. This is the effect of setting "fixed" equal to "true".

Screen tooltip example1 20090209.png

The last screenshot shows the different styling for the ".hasTip3" tooltip, based on the "custom3-" CSS class styles.

Screen tooltip example3 20090210.png

More Information

More information about the topics covered in this tutorial is available at the links below.