Bootstrap Tooltip Plugin
Tooltips are very useful when you want to describe a link. The Tooltip plugin was inspired by Jason Frame written by jQuery.tipsy inspiration. The Tooltip plugin has made many improvements, such as not depending on images but using CSS for animation effects, and using data attributes to store title information.
If you want to reference the plugin's functionality separately, you need to include tooltip.js. Or, as Bootstrap Plugins Overview chapter mentions, you can include bootstrap.js or the compressed version bootstrap.min.js。
Usage
The Tooltip plugin generates content and markup on demand, and by default places the tooltip after its trigger element. You can add a tooltip in the following two ways:
- Via data attributes: To add a tooltip, simply add to an anchor tag data-toggle="tooltip" . The anchor's title is the tooltip text. By default, the plugin places the tooltip on top.
请悬停在我的上面
$('#identifier').tooltip(options)
Unlike the dropdowns and other plugins discussed earlier, the Tooltip plugin is not a pure CSS plugin. To use it, you must activate it with jQuery (read JavaScript). Use the script below to enable all tooltips on the page:
$(function () { $("[data-toggle='tooltip']").tooltip(); });
Example
The following example demonstrates using the Tooltip plugin via data attributes.
Example
<
h4
Tooltip Plugin - Anchor
h4
This is a
<a href="" class="tooltip-test" data-toggle="tooltip" title=">Default Tooltip
"
Default Tooltip
a
. This is a
<a href="" class="tooltip-test" data-toggle="tooltip" data-placement="left" title=">Tooltip on the left
"
Tooltip on the left
a
. This is a
<a href="" data-toggle="tooltip" data-placement="top" title=">Tooltip on top
"
Tooltip on top
a
. This is a
<a href="" data-toggle="tooltip" data-placement="bottom" title=">Tooltip on the bottom
"
Tooltip on the bottom
a
. This is a
<a href="" data-toggle="tooltip" data-placement="right" title=">Tooltip on the right
"
Tooltip on the right
a
<
br
<
h4Tooltip Plugin - Buttons
h4
<button type="button" class=">btn btn-default
"
data-toggle
=
"
tooltip
"
title
=
"
Default Tooltip
"
Default Tooltip
button
<button type="button" class=">btn btn-default
"
data-toggle
=
"
tooltip
"
data-placement
=
"
left
"
title
=
"
Tooltip on the left
"
Tooltip on the left
button
<button type="button" class=">btn btn-default
"
data-toggle
=
"
tooltip
"
data-placement
=
"
top
"
title
=
"
Tooltip on top
"
Tooltip on top
button
<button type="button" class=">btn btn-default
"
data-toggle
=
"
tooltip
"
data-placement
=
"
bottom
"
title
=
"
Tooltip on the bottom
"
Tooltip on the bottom
button
<button type="button" class=">btn btn-default
"
data-toggle
=
"
tooltip
"
data-placement
=
"
right
"
title
=
"
Tooltip on the right
"
Tooltip on the right
button
<script$(function(){$("[data-toggle='tooltip']" )>.
tooltip
(
)
;
}
)
;
script
Try it »
The result is as follows:
Option
Some options are added through the Bootstrap Data API (Bootstrap Data API) or called via JavaScript. The following table lists these options:
| Option name | Type / Default value | Data attribute name | Description |
|---|---|---|---|
| animation | boolean | ||
| Default value: true | data-animation | The tooltip uses a CSS gradient filter effect. | |
| html | boolean | ||
| Default value: false | data-html | Insert HTML into the tooltip. If false, jQuery's text method will be used to insert content into the DOM. If you are concerned about XSS attacks, use text. | |
| placement | string|function | ||
| Default value: top | data-placement | Specifies how to position the tooltip (i.e., top|bottom|left|right|auto). | |
| When specified as auto , the tooltip is dynamically adjusted. For example, if placement is "auto left", the tooltip will be shown on the left whenever possible, and only on the right when conditions do not allow it. | |||
| selector | string | ||
| Default value: false | data-selector | If a selector is provided, the tooltip object will be delegated to the specified target. | |
| title | string | function | ||
| Default value: '' | data-title | If the title attribute is not specified, the title option is the default title value. | |
| trigger | string | ||
| Default value: 'hover focus' | data-trigger | Defines how the tooltip is triggered: ** click| hover | focus | manual**. You can pass multiple triggers, separated by spaces. | |
| delay | number | object | ||
| Default value: 0 | data-delay | The number of milliseconds to delay showing and hiding the tooltip - not applicable to the manual trigger type. If a number is provided, the delay applies to both showing and hiding. If an object is provided, its structure is as follows: |
delay: { show: 500, hide: 100 }| onSelect | event | container | string | false
Default value: false | data-container | Append the tooltip to the specified element.
Example: container: 'body' |
Method
Below are some useful methods of the Tooltip plugin:
| Method | Description | Example |
|---|---|---|
| Options: .tooltip(options) | Attaches a tooltip handler to a collection of elements. | |
| $().tooltip(options) | ||
| Toggle: .tooltip('toggle') | Toggles the tooltip of an element. |
$('#element').tooltip('toggle')| onSelect | event | Show: .tooltip('show') | Shows the element's tooltip. |
$('#element').tooltip('show')
| onSelect | event | Hide: .tooltip('hide') | Hides the element's tooltip. |
$('#element').tooltip('hide')
| onSelect | event | Destroy: .tooltip('destroy') | Hides and destroys the element's tooltip. |
$('#element').tooltip('destroy')
|
Example
The following example demonstrates the use of Tooltip plugin methods.
Example
<div style=">padding: 100px 100px 10px;
"
This is a
<a href="" class="tooltip-show" data-toggle="tooltip" title="show">Tooltip method show
a
. This is a
<a href="" class="tooltip-hide" data-toggle="tooltip" data-placement="left" title="hide">Tooltip method hide
a
. This is a
<a href="" class="tooltip-destroy" data-toggle="tooltip" data-placement="top" title="destroy">Tooltip method destroy
a
. This is a
<a href="" class="tooltip-toggle" data-toggle="tooltip" data-placement="bottom" title="toggle">Tooltip method toggle
a
.
<br>
<br>
<br>
<br>
<br>
<br>
<p class="tooltip-options">This is a
<a href="" data-toggle="tooltip" title="">Tooltip method options
a
.
p
<script$(function(){$('.tooltip-show' )>.
tooltip
(
'
show
'
)
;
}
)
; $
(
function
(
)
{
$
(
'
.tooltip-hide
'
)
.
tooltip
(
'
hide
'
)
;
}
)
; $
(
function
(
)
{
$
(
'
.tooltip-destroy
'
)
.
tooltip
(
'
destroy
'
)
;
}
)
; $
(
function
(
)
{
$
(
'
.tooltip-toggle
'
)
.
tooltip
(
'
toggle
'
)
;
}
)
; $
(
function
(
)
{
$
(
"
.tooltip-options a
"
)
.
tooltip
(
{
html
:
true
}
)
;
}
)
;
script
div
Try it »
The result is as follows:
Events
The table below lists the events used in the Tooltip plugin. These events can be used as hooks in functions.
| Events | Description | Example |
|---|---|---|
| show.bs.tooltip | This event is fired immediately when the show instance method is called. |
$('#myTooltip').on('show.bs.tooltip', function () { // do something... })| onSelect | event | shown.bs.tooltip | This event fires when the tooltip becomes visible to the user (it waits for CSS transition effects to complete). |
$('#myTooltip').on('shown.bs.tooltip', function () {
// Perform some actions...
})
| onSelect | event | hide.bs.tooltip | This event fires immediately when the hide instance method is called. |
$('#myTooltip').on('hide.bs.tooltip', function () {
// Perform some actions...
})
| onSelect | event | hidden.bs.tooltip | This event fires when the tooltip has been hidden from the user (it waits for CSS transition effects to complete). |
$('#myTooltip').on('hidden.bs.tooltip', function () {
// Perform some actions...
})
|
Example
The following example demonstrates the use of Tooltip plugin events.
Example
<
h4
Tooltip Plugin - Anchor
h4
This is a
<a href="" class="tooltip-show" data-toggle="tooltip" title=">Default Tooltip
"
Default Tooltip
a
.
<script$(function(){$('.tooltip-show' )>.
tooltip
(
'
show
'
)
;
}
)
; $
(
function
(
)
{
$
(
'
.tooltip-show
'
)
.
on
(
'
show.bs.tooltip
'
,
function
(
)
{
alert
(
"
Alert message on show
"
)
;
}
)
}
)
;
script
Try it »
The result is as follows:
If you want to reference the plugin's functionality separately, you need to include tooltip.js. Or, as