Tooltipster

A powerful, flexible jQuery plugin enabling you to easily create semantic, modern tooltips enhanced with the power of CSS.

Demos

Getting Started

1. Load jQuery and include Tooltipster's plugin files

After you download Tooltipster, move tooltipster.css and jquery.tooltipster.min.js to your root's CSS and JavaScript directories. Next, load jQuery and include Tooltipster's CSS and JavaScript files inside of your tags:

<head>
...

    <link rel="stylesheet" type="text/css" href="css/tooltipster.css" />

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="js/jquery.tooltipster.min.js"></script>

...
</head>

2. Set up your HTML

In order for Tooltipster to work, we first need to add the .tooltip class (or whatever class / means of selection you'd like to use) to whatever element we wish to have a tooltip. Next, we'll set the title attribute to whatever we'd like our tooltip to say. Here are a few examples:

Adding a tooltip to an image:

<img src="my-image.png" class="tooltip" title="This is my image's tooltip message!" />

Adding a tooltip to a link that already has a class:

<a href="http://calebjacob.com" class="ketchup tooltip" title="This is my link's tooltip message!">Link</a>

Adding a tooltip to a div:

<div class="tooltip" title="This is my div's tooltip message!"> 
    <p>This div has a tooltip when you hover over it!</p>
</div>

3. Activate Tooltipster

The last thing we have to do is activate the plugin. To do this, add the following script right before your closing </head> tag (using whatever selector you'd like - in this case we're using the .tooltip class):

<head>

...

    <script>
        $(document).ready(function() {
            $('.tooltip').tooltipster();
        });
    </script>
</head>

4. Taking It a Step Further

Using HTML tags inside your tooltips

Tooltipster allows you to use any HTML tag you can think of inside your tooltips. This means you can insert things like images and text formatting tags. However, when inserting elements such as images, make sure to set specific width and height attributes (and use single quotes when setting attributes):

<a href="#" class="tooltip" title="<img src='my-image.png' width='100' height='100' /> Here is an image and <b>bold text!</b>">Link</a>

Customizing Your Tooltipster's Style

The style of your Tooltipsters can be easily changed by modifying the default Tooltipster theme in the tooltipster.css file. You also have the option to create and attach a brand new theme (comes in handy if you want to use more than one theme at once). To do this, start with this example and add your flair:

.my-custom-theme {
	border-radius: 5px; 
	border: 2px solid #000;
	background: #4c4c4c;
	color: #fff;
}

/* Use this next selector to style things like font-size and line-height: */
.my-custom-theme .tooltipster-content {
	font-family: Arial, sans-serif;
	font-size: 14px;
	line-height: 16px;
	padding: 8px 10px;
}

After you have created your new theme, don't forget to specify it in your Tooltipster settings:

$('.tooltip').tooltipster({
    theme: '.my-custom-theme'
});

Customizing Tooltipster's Functionality

Tooltipster's options gives you a wide range of variables to tweak your tooltip to your heart's content. To learn more about what each of these options do, read the documentation section. Here's all of the available, default options:

$('.tooltip').tooltipster({
   animation: 'fade',
   arrow: true,
   arrowColor: '',
   content: '',
   delay: 200,
   fixedWidth: 0,
   maxWidth: 0,
   functionBefore: function(origin, continueTooltip) {
      continueTooltip();
   },
   functionReady: function(origin, tooltip) {},
   functionAfter: function(origin) {},
   icon: '(?)',
   iconDesktop: false,
   iconTouch: false,
   iconTheme: '.tooltipster-icon',
   interactive: false,
   interactiveTolerance: 350,
   offsetX: 0,
   offsetY: 0,
   onlyOne: true,
   position: 'top',
   speed: 350,
   timer: 0,
   theme: '.tooltipster-default',
   touchDevices: true,
   trigger: 'hover',
   updateAnimation: true
});

Options

animation

fade, grow, swing, slide, fall Determines how the tooltip will animate in and out. Feel free to modify or create custom transitions in the tooltipster.css file. In IE9 and 8, all animations default to a JavaScript generated, fade animation. Default: 'fade'

arrow

boolean Adds the "speech bubble arrow" to the tooltip. Default: true

arrowColor

hex code / rgb Select a specific color for the "speech bubble arrow". Default: will inherit the tooltip's background color

content

string If set, this will override the content of the tooltip. Default: ''

delay

integer Delay how long it takes (in milliseconds) for the tooltip to start animating in. Default: 200

fixedWidth

integer Set a fixed width for the tooltip. The tooltip will always be a consistent width - no matter your content size. Default: 0 (auto width)

maxWidth

integer Set a max width for the tooltip. If the tooltip ends up being smaller than the set max width, the tooltip's width will be set automatically. Default: 0 (no max width)

functionBefore

function Create a custom function to be fired before the tooltip launches. See the advanced section to learn more. Default: function(origin, continueTooltip) { continueTooltip(); }

functionReady

function Create a custom function to be fired when the tooltip and its contents have been added to the DOM. Default: function(origin, tooltip) {}

functionAfter

function Create a custom function to be fired once the tooltip has been closed and removed from the DOM. Default: function(origin) {}

icon

string If using the iconDesktop or iconTouch options, this sets the content for your icon. Default: '(?)'

iconTheme

CSS class If using the iconDesktop or iconTouch options, this sets the class on the icon (used to style the icon). Default: '.tooltipster-icon'

iconDesktop

boolean Generate an icon next to your content that is responsible for activating the tooltip on non-touch devices. Default: false

iconTouch

boolean Generate an icon next to your content that is responsible for activating the tooltip on touch devices (tablets, phones, etc). Default: false

interactive

boolean Create a delay that allows users to interact with the tooltip. The tooltip will close after the user hovers off the tooltip or taps off of it. Default: false

interactiveTolerance

boolean If the tooltip is interactive and activated by a hover event, set the amount of time (milliseconds) allowed for a user to hover off of the tooltip activator (origin) on to the tooltip itself - keeping the tooltip from closing. Default: 350

offsetX

integer Offsets the tooltip (in pixels) farther left/right from the origin. Default: 0

offsetY

integer Offsets the tooltip (in pixels) farther up/down from the origin. Default: 0

onlyOne

boolean If true, only one tooltip will be allowed to be active at a time. Default: true

position

right, left, top, top-right, top-left, bottom, bottom-right, bottom-left Set the position of the tooltip. Default: 'top'

speed

integer Set the speed of the animation. Default: 350

timer

integer How long the tooltip should be allowed to live before closing. Default: 0 (disabled)

theme

CSS class Set the theme used for your tooltip. Default: '.tooltipster-default'

touchDevices

boolean If true, Tooltipster will run on touch devices (tablets, phones, etc). Default: true

trigger

hover, click, custom Set how tooltips should be activated and closed. See the advanced section to learn how to build custom triggers. Default: 'hover'

updateAnimation

boolean If a tooltip is open while its content is updated, play a subtle animation when the content changes. Default: true

Advanced

The awesomesauce Tooltipster API

The Tooltipster API was created to be as flexible and easy to use as possible (thanks to glebtv for the inspiration). The API allows you to create custom triggers, update tooltip content on the fly (whether the tooltip is currently open or not), destroy Tooltipster functionality if needed, and reposition tooltips for unique use cases.

Here is a quick look at all of the API methods:

// show a tooltip
$(...).tooltipster('show');

// hide a tooltip
$(...).tooltipster('hide');

// temporarily disable a tooltip from being able to open
$(...).tooltipster('disable');

// if a tooltip was disabled from opening, reenable its previous functionality
$(...).tooltipster('enable');

// hide and destroy tooltip functionality
$(...).tooltipster('destroy');

// update tooltip content
$(...).tooltipster('update', 'My new content');

// reposition and resize the tooltip
$(...).tooltipster('reposition');

It's important to note that you never select the tooltip itself to run these methods. You should either select elements that tooltips originate from, or select a container to apply the method to all tooltip origins inside. The following examples are valid:

// show all tooltips belonging to origins with the class .tooltip
$('.tooltip').tooltipster('show');

// hide all open tooltips in the body
$('body').tooltipster('hide');

// update a single tooltip's content (whether the tooltip is open or not)
$('#my-unique-origin').tooltipster('update', 'My new content');

Using AJAX to generate your tooltip content

Tooltipster gives you the ability to fire a custom synchronous or asynchronous function before the tooltip launches. One great use for this is to grab dynamic content for your tooltips via AJAX. If you write a little extra code, you can also cache the content received from your AJAX call for the next time the tooltip opens.

Here is an example of using functionBefore() asynchronously to replace a loading notification with our AJAX call. Once the content has been retrieved and updated, we'll take advantage of setting $.data() to keep our content cached for the next time the tooltip is opened:

$('.tooltip').tooltipster({
   content: 'Loading...',
   functionBefore: function(origin, continueTooltip) {
   
      // we'll make this function asynchronous and allow the tooltip to go ahead and show the loading notification while fetching our data
      continueTooltip();
        
      // next, we want to check if our data has already been cached
      if (origin.data('ajax') !== 'cached') {
         $.ajax({
            type: 'POST',
            url: 'example.php',
            success: function(data) {
               // update our tooltip content with our returned data and cache it
               origin.tooltipster('update', data).data('ajax', 'cached');
            }
         });
      }
   }
});

Manually launch and hide a tooltip

Tooltipster gives you complete freedom with how you'd like to open and close tooltips. You do not need to set your trigger option to 'custom' for this to work. If you have your trigger set to 'hover', the tooltip can be triggered by custom events as well as have the default hover functionality.

Here's an example of how you could launch a specific tooltip on page load and close it when any key on your keyboard is pressed. This will still preserve the default hover trigger.

<span class="tooltip" id="example" title="My tooltip content">Example</span>
// first, launch the Tooltipster plugin
$(document).ready(function() {
   $('.tooltip').tooltipster();
});
   
// on window load, show the tooltip
$(window).load(function() {
   $('#example').tooltipster('show');
      
   // once a key is pressed on your keyboard, hide the tooltip
   $(window).keypress(function() {
      $('#example').tooltipster('hide');
   });
});

Updating a tooltip's content

It's easy as pie to update a tooltip's content - whether it's open or closed. Depending on your selector, you can update multiple tooltips at once or just one:

$('#my-special-tooltip').tooltipster('update', 'My new content');

By default, Tooltipster will play a subtle animation when the content changes. To tweak the animation, check out the '.tooltipster-content-changing' class in your tooltipster.css file. It's important to note that only CSS transforms will be animated. To disable this animation, set updateAnimation to false.

Community submitted use cases

Using Tooltipster alongside jQuery Validate by sparky672

Did you do something awesome with Tooltipster? Tell me and I'll post it up here!

Themes

Tired of the same old? Download some rocking themes for your Tooltipsters (or create and share your own)! Click to download.

Talk

Share thoughts, questions, & bugs!

comments powered by Disqus