The template includes a complete and powerful tooltip functionality, which can be used just by adding a class or through an API. This plugin also provides a contextual menu method.
The basic use of the tooltip plugin is to replace the browser's built-in tooltips. To use it, just define a title for your element and add one of those classes either to the element or to its parent:
<p class="with-tooltip" title="This is a tooltip"> Hover me! </p>
Example using inline options:
<p class="with-tooltip" title="This is a tooltip" data-tooltip-options='{"classes":["anthracite-gradient"],"position":"bottom"}'> Hover me! </p>
<ul class="children-tooltip"> <li title="Tooltip for element 1">Element 1</li> <li title="Tooltip for element 2">Element 2</li> <li title="Tooltip for element 3">Element 3</li> <li title="Tooltip for element 4">Element 4</li> </ul>
It is easy to show a tooltip on any element with a simple call:
$(selector).tooltip('Tooltip message', { /* Options */ });
The content may be raw text, html, a jQuery selection or a function to run on the element and which should return the content. A wide range of options are available to customize the look and behavior of the tooltip. See methods/options references below for more details.
Once a tooltip is open, you can change its content just by calling the same method again. No need to specify options again, they will be pulled from the previous tooltip. If you specify options, they will erase the previous options.
// Open tooltip $(selector).tooltip('Tooltip message', { /* Options */ }); // Change content $(selector).tooltip('New message');
To remove a tooltip, just call:
$(selector).removeTooltip();
Note that if the tooltip content was pulled from the DOM, it will be restored when removing the tooltip, thus allowing multiple calls.
The tooltip content may be loaded using ajax: simply provide an url or a promise object returned by an $.ajax() call:
<button class="button with-tooltip" title="Loading..." data-tooltip-options='{"ajax":"content-url.html"}'> Text </button>
$(selector).tooltip('Loading...', { ajax: 'content-url.html', ajaxOptions: { type: 'POST' }, ajaxErrorMessage: 'Unable to load tooltip' });
$(selector).tooltip('Loading...', { ajax: $.ajax('content-url.html', { type: 'POST' }), ajaxErrorMessage: 'Unable to load tooltip' });
The tooltip plugin provides a menu functionality: when the user clicks the desired element, a tooltip is displayed with any custom content (it may be a menu or anything else), and it closes when the user clicks anywhere else.
<button id="menu-tooltip" type="button" class="button full-width">Click me!</button> <div id="menu-content-block"> <select class="select multiple white-gradient easy-multiple-selection check-list" multiple> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3" selected="selected">Selected option</option> <option value="4">Option 3</option> </select> </div>
$('#menu-tooltip').menuTooltip($('#menu-content-block').hide(), { classes: ['anthracite-gradient', 'with-small-padding'] });
In this example, the content is pulled from the DOM just by giving the corresponding jQuery selection to the function. It is detached from its original location, shown as it was hidden, and when the menu closes, it is re-inserted into its original location and hidden back. No complex callbacks, everything is built-in - but of course, the tooltips callbacks can be used if needed.
As for tooltips, the content may be text, html, a jQuery selection or a function to run on the element.
As for tooltips, menus can load load some content using ajax, simply provide an url or a promise object:
$(selector).menuTooltip('Loading menu...', { ajax: 'menu-content.html' });
Display a tooltip over an element. If the page is not yet ready, delay the tooltip until it is ready.
@var string|function|jQuery content a text or html content to display, or a function to run on the element to get the content (can be omitted, auto-detect if not defined or empty)
@var object options an object with any options for the tooltip - optional (see defaults for more details). If not set, the function will try to retrieve any option of an existing or delayed tooltip on the same element, so when changing the content of a tooltip just call the function without options
Remove tooltip
@param boolean force use true to close tooltips even when the onClose/onAbort callback functions return false (optional, default: false)
@param boolean skipAnimation use true to disable the close animation (optional, default: false)
Open a tooltip menu on click on any element
@var string|function|jQuery content a text or html content to display, or a function to run on the element to get the content
@var object options an object with any options for the tooltip - optional (see defaults for more details)
@var string eventName the event on which to open the menu - optional (default: 'click')